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, and BCC).

For example:

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

It will add the three 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.example"),
                               InternetAddress.parse("abc@def.example"),
                               InternetAddress.parse("ghi@abc.example")};
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.example,abc@def.example,ghi@abc.example"));

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)