I want to send a message to multiple Recipients using following the method :
message.addRecipient(Message.RecipientType.TO, String arg1);
<
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);
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"));
Easy way to do
String[] listofIDS={"ramasamygms@gmail.com","ramasamycse94@gmail.com"};
for(String cc:listofIDS) {
message.addRecipients(Message.RecipientType.CC,InternetAddress.parse(cc));
}
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.
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];
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"));