Send Mail to multiple Recipients in java
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"));
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);
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"));