-->

Send Mail to multiple Recipients in java

2019-01-10 10:39发布

问题:

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.

回答1:

If you invoke addRecipient multiple times it will add the given recipient to the list of recipients of the given time (TO, 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


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

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

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


回答2:

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:

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:

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));


回答5:

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

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"));


回答6:

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



回答7:

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

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. If strict is true, many (but not all) of the RFC822 syntax rules for emails are enforced.

    msg.setRecipients(Message.RecipientType.CC,
    InternetAddress.parse(to, true));
    
  3. 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));
    


回答8:

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:

You can use n-number of recipient below method:

  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:

If you want to send as Cc using MimeMessageHelper

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.