用java向多个收件人发送邮件

时间:2021-10-30 18:18:34

I want to send message to multiple Recipients using following method ::

我想通过以下方式向多个收件人发送消息:

message.addRecipient(Message.RecipientType.TO, String arg1);

OR

message.setRecipients(Message.RecipientType.TO,String arg1);

But one confusion is that in Second arguement, How to pass multiple addresses like :

但有一个困惑是,在第二个争论中,如何传递多个地址,比如:

message.addRecipient(Message.RecipientType.CC, "abc@abc.com,abc@def.com,ghi@abc.com");

OR

message.addRecipient(Message.RecipientType.CC, "abc@abc.com;abc@def.com;ghi@abc.com");

I can send message using alternate methods too, but want to know the purpose of above method. If i cant use it(as till now i haven't got any answer for above requirement) then what is the need for this method to be in mail API.

我也可以使用其他方法发送消息,但是我想知道上述方法的目的。如果我不能使用它(到目前为止,我还没有对上面的要求作出任何回答),那么这个方法在mail API中有什么需要呢?

10 个解决方案

#1


86  

If you invoke addRecipient multiple times it will add the given recipient to the list of recipients of the given time (TO, CC, BCC)

如果您多次调用addRecipient,它会将给定的收件人添加到给定时间的收件人列表(到,CC, BCC)

For example:

例如:

message.addRecipient(Message.RecipientType.CC, InternetAddress.parse("abc@abc.com"));
message.addRecipient(Message.RecipientType.CC, InternetAddress.parse("abc@def.com"));
message.addRecipient(Message.RecipientType.CC, InternetAddress.parse("ghi@abc.com"));

Will add the 3 addresses to CC

将这3个地址添加到CC吗


If you wish to add all addresses at once you should use setRecipients or addRecipients and provide it with an array of addresses

如果您希望同时添加所有地址,您应该使用setrepients或addRecipients,并为其提供一个地址数组

Address[] cc = new Address[] {InternetAddress.parse("abc@abc.com"),
                               InternetAddress.parse("abc@def.com"), 
                               InternetAddress.parse("ghi@abc.com")};
message.addRecipients(Message.RecipientType.CC, cc);

You can also use InternetAddress.parse to parse a list of addresses

你也可以使用InternetAddress。解析以解析一个地址列表。

message.addRecipients(Message.RecipientType.CC, 
                      InternetAddress.parse("abc@abc.com,abc@def.com,ghi@abc.com"));

#2


20  

Hi every one this code is workin for me please try with this for sending mail to multiple recepients

大家好,这段代码适用于我,请尝试将邮件发送到多个收件人

private String recipient = "yamabs@gmail.com ,priya@gmail.com ";
String[] recipientList = recipient.split(",");
InternetAddress[] recipientAddress = new InternetAddress[recipientList.length];
int counter = 0;
for (String recipient : recipientList) {
    recipientAddress[counter] = new InternetAddress(recipient.trim());
    counter++;
}
message.setRecipients(Message.RecipientType.TO, recipientAddress);

#3


10  

Try this way:

试试这种方法:

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("mail3@mail.com"));
String address = "mail@mail.com,mail2@mail.com";
InternetAddress[] iAdressArray = InternetAddress.parse(address);
message.setRecipients(Message.RecipientType.CC, iAdressArray);

#4


8  

Just use the method message.setRecipients with multiple addresses separated by commas:

只需使用方法消息。用逗号分隔的多个地址的setRecipients:

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("abc@abc.com,abc@def.com,ghi@abc.com"));

message.setRecipients(Message.RecipientType.CC, InternetAddress.parse("abc@abc.com,abc@def.com,ghi@abc.com"));

works fine with only one address too

使用一个地址也可以

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("abc@abc.com"));

#5


7  

You can have multiple addresses separated by comma

可以用逗号分隔多个地址

if (cc.indexOf(',') > 0)
    message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));   
else
    message.setRecipient(Message.RecipientType.CC, new InternetAddress(cc));

#6


5  

InternetAddress.Parse is going to be your friend! See the worked example below:

InternetAddress。Parse将会是你的朋友!参见下面的示例:

String to = "med@joe.com, maz@frank.com, jezz@jam.com";
String toCommaAndSpaces = "med@joe.com maz@frank.com, jezz@jam.com";
  1. Parse a comma-separated list of email addresses. Be strict. Require comma separated list.
  2. 解析以逗号分隔的电子邮件地址列表。是严格的。需要逗号分隔的列表。
  3. If strict is true, many (but not all) of the RFC822 syntax rules for emails are enforced.

    如果严格是真的,那么会强制执行许多(但不是全部)RFC822电子邮件语法规则。

    msg.setRecipients(Message.RecipientType.CC,
    InternetAddress.parse(to, true));
    
  4. Parse comma/space-separated list. Cut some slack. We allow spaces seperated list as well, plus invalid email formats.

    解析逗号或空格分隔的列表。减少一马。我们还允许空间分隔列表,以及无效的电子邮件格式。

    msg.setRecipients(Message.RecipientType.BCC,
    InternetAddress.parse(toCommaAndSpaces, false));
    

#7


4  

So ... it took many months, but still ... You can send email to multiple recipients by using the ',' as separator and

所以…花了好几个月,但还是……您可以使用“,”作为分隔符和

message.setRecipients(Message.RecipientType.CC, "abc@abc.com,abc@def.com,ghi@abc.com");

is ok. At least in JavaMail 1.4.5

就可以了。至少在JavaMail 1.4.5中是这样

#8


2  

String[] mailAddressTo = new String[3];    
mailAddressTo[0] = emailId_1;    
mailAddressTo[1] = emailId_2;    
mailAddressTo[2] = "xyz@gmail.com";

InternetAddress[] mailAddress_TO = new InternetAddress[mailAddressTo.length];

for (int i = 0; i < mailAddressTo.length; i++)
{
    mailAddress_TO[i] = new InternetAddress(mailAddressTo[i]);
}

message.addRecipients(Message.RecipientType.TO, mailAddress_TO);ress_TO = new InternetAddress[mailAddressTo.length]; 

#9


1  

You can use n-number of recipient below method:

你可以使用n号收件人以下的方法:

  String to[] = {"a@gmail.com"} //Mail id you want to send;
  InternetAddress[] address = new InternetAddress[to.length];
  for(int i =0; i< to.length; i++)
  {
      address[i] = new InternetAddress(to[i]);
  }

   msg.setRecipients(Message.RecipientType.TO, address);

#10


1  

If you want to send as Cc using MimeMessageHelper

如果您想使用MimeMessageHelper以Cc发送

List<String> emails= new ArrayList();
email.add("email1");
email.add("email2");
for (String string : emails) {
message.addCc(string);
}

Same you can use to add multiple recipient.

同样,您可以使用添加多个收件人。

#1


86  

If you invoke addRecipient multiple times it will add the given recipient to the list of recipients of the given time (TO, CC, BCC)

如果您多次调用addRecipient,它会将给定的收件人添加到给定时间的收件人列表(到,CC, BCC)

For example:

例如:

message.addRecipient(Message.RecipientType.CC, InternetAddress.parse("abc@abc.com"));
message.addRecipient(Message.RecipientType.CC, InternetAddress.parse("abc@def.com"));
message.addRecipient(Message.RecipientType.CC, InternetAddress.parse("ghi@abc.com"));

Will add the 3 addresses to CC

将这3个地址添加到CC吗


If you wish to add all addresses at once you should use setRecipients or addRecipients and provide it with an array of addresses

如果您希望同时添加所有地址,您应该使用setrepients或addRecipients,并为其提供一个地址数组

Address[] cc = new Address[] {InternetAddress.parse("abc@abc.com"),
                               InternetAddress.parse("abc@def.com"), 
                               InternetAddress.parse("ghi@abc.com")};
message.addRecipients(Message.RecipientType.CC, cc);

You can also use InternetAddress.parse to parse a list of addresses

你也可以使用InternetAddress。解析以解析一个地址列表。

message.addRecipients(Message.RecipientType.CC, 
                      InternetAddress.parse("abc@abc.com,abc@def.com,ghi@abc.com"));

#2


20  

Hi every one this code is workin for me please try with this for sending mail to multiple recepients

大家好,这段代码适用于我,请尝试将邮件发送到多个收件人

private String recipient = "yamabs@gmail.com ,priya@gmail.com ";
String[] recipientList = recipient.split(",");
InternetAddress[] recipientAddress = new InternetAddress[recipientList.length];
int counter = 0;
for (String recipient : recipientList) {
    recipientAddress[counter] = new InternetAddress(recipient.trim());
    counter++;
}
message.setRecipients(Message.RecipientType.TO, recipientAddress);

#3


10  

Try this way:

试试这种方法:

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("mail3@mail.com"));
String address = "mail@mail.com,mail2@mail.com";
InternetAddress[] iAdressArray = InternetAddress.parse(address);
message.setRecipients(Message.RecipientType.CC, iAdressArray);

#4


8  

Just use the method message.setRecipients with multiple addresses separated by commas:

只需使用方法消息。用逗号分隔的多个地址的setRecipients:

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("abc@abc.com,abc@def.com,ghi@abc.com"));

message.setRecipients(Message.RecipientType.CC, InternetAddress.parse("abc@abc.com,abc@def.com,ghi@abc.com"));

works fine with only one address too

使用一个地址也可以

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("abc@abc.com"));

#5


7  

You can have multiple addresses separated by comma

可以用逗号分隔多个地址

if (cc.indexOf(',') > 0)
    message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));   
else
    message.setRecipient(Message.RecipientType.CC, new InternetAddress(cc));

#6


5  

InternetAddress.Parse is going to be your friend! See the worked example below:

InternetAddress。Parse将会是你的朋友!参见下面的示例:

String to = "med@joe.com, maz@frank.com, jezz@jam.com";
String toCommaAndSpaces = "med@joe.com maz@frank.com, jezz@jam.com";
  1. Parse a comma-separated list of email addresses. Be strict. Require comma separated list.
  2. 解析以逗号分隔的电子邮件地址列表。是严格的。需要逗号分隔的列表。
  3. If strict is true, many (but not all) of the RFC822 syntax rules for emails are enforced.

    如果严格是真的,那么会强制执行许多(但不是全部)RFC822电子邮件语法规则。

    msg.setRecipients(Message.RecipientType.CC,
    InternetAddress.parse(to, true));
    
  4. Parse comma/space-separated list. Cut some slack. We allow spaces seperated list as well, plus invalid email formats.

    解析逗号或空格分隔的列表。减少一马。我们还允许空间分隔列表,以及无效的电子邮件格式。

    msg.setRecipients(Message.RecipientType.BCC,
    InternetAddress.parse(toCommaAndSpaces, false));
    

#7


4  

So ... it took many months, but still ... You can send email to multiple recipients by using the ',' as separator and

所以…花了好几个月,但还是……您可以使用“,”作为分隔符和

message.setRecipients(Message.RecipientType.CC, "abc@abc.com,abc@def.com,ghi@abc.com");

is ok. At least in JavaMail 1.4.5

就可以了。至少在JavaMail 1.4.5中是这样

#8


2  

String[] mailAddressTo = new String[3];    
mailAddressTo[0] = emailId_1;    
mailAddressTo[1] = emailId_2;    
mailAddressTo[2] = "xyz@gmail.com";

InternetAddress[] mailAddress_TO = new InternetAddress[mailAddressTo.length];

for (int i = 0; i < mailAddressTo.length; i++)
{
    mailAddress_TO[i] = new InternetAddress(mailAddressTo[i]);
}

message.addRecipients(Message.RecipientType.TO, mailAddress_TO);ress_TO = new InternetAddress[mailAddressTo.length]; 

#9


1  

You can use n-number of recipient below method:

你可以使用n号收件人以下的方法:

  String to[] = {"a@gmail.com"} //Mail id you want to send;
  InternetAddress[] address = new InternetAddress[to.length];
  for(int i =0; i< to.length; i++)
  {
      address[i] = new InternetAddress(to[i]);
  }

   msg.setRecipients(Message.RecipientType.TO, address);

#10


1  

If you want to send as Cc using MimeMessageHelper

如果您想使用MimeMessageHelper以Cc发送

List<String> emails= new ArrayList();
email.add("email1");
email.add("email2");
for (String string : emails) {
message.addCc(string);
}

Same you can use to add multiple recipient.

同样,您可以使用添加多个收件人。