1

I am trying to send multiple mails using JAVA Mail -

When I add a single Recipient -

message.addRecipient(Message.RecipientType.TO, new InternetAddress(“abc@xyz.com”));

It works fine, but not when I add multiple email addresses -

Here is the code

message.addRecipient(Message.RecipientType.TO, new InternetAddress(“abc@xyz.com”));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(“def@xyz.com"));

message.addRecipient(Message.RecipientType.CC, new InternetAddress(“ghi@xyz.com"));
message.addRecipient(Message.RecipientType.CC, new InternetAddress(“jkl@xyz.com"));

message.addRecipient(Message.RecipientType.BCC, new InternetAddress(“mno@xyz.com"));

The mail is sent and received, but when I check the email of abc@xyz.com I can't see that the email has also been sent to def@xyz.com or vise versa. Neither I can see CC in the list.

Mail details from abc@xyz.com

from:   xyz@xyz.com
to: abc@xyz.com
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test

Mail details from def@xyz.com

from:   xyz@xyz.com
to: def@xyz.com
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test

Mail details from ghi@xyz.com

from:   xyz@xyz.com
to: ghi@xyz.com
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test

Mail details from jkl@xyz.com

from:   xyz@xyz.com
to: jkl@xyz.com
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test

I tried changing the logic a little, but same result -

message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(“abc@xyz.com, def@xyz.com"));

message.addRecipient(Message.RecipientType.CC, InternetAddress.parse(“ghi@xyz.com, jkl@xyz.com”));

message.addRecipient(Message.RecipientType.BCC, InternetAddress.parse(“mno@xyz.com"));

I am expecting to see the details as -

from:   xyz@xyz.com
to: abc@xyz.com, def@xyz.com
cc: ghi@xyz.com, jkl@xyz.com
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Bishwarup Das
  • 681
  • 1
  • 12
  • 21
  • 1
    Can you post a [mcve]? Related: http://stackoverflow.com/questions/13854037/send-mail-to-multiple-recipients-in-java Also note that there is some "quote" issue in your posted code (`“` vs `"`) –  Sep 08 '16 at 11:30
  • Please avoid the quote, its actually " – Bishwarup Das Sep 08 '16 at 11:36

2 Answers2

1

You should try:

Address[] toArray = new Address[] {InternetAddress.parse("abc@xyz.com"),
                               InternetAddress.parse("def@xyz.com")};
message.addRecipients(Message.RecipientType.TO, toArray);
Martijn Burger
  • 7,315
  • 8
  • 54
  • 94
  • I tried this - `Address[] mail_To = new Address[] {new InternetAddress("abc@xyz.com"), new InternetAddress("efg@xyz.com")}; message.setRecipients(Message.RecipientType.TO, mail_To);` It didn't work. I am receiving individual mails, can't see who all are in the list – Bishwarup Das Sep 08 '16 at 12:10
1

To prevent any mistakes and surprises, I would recommend to use setRecipients(Message.RecipientType type, Address[] addresses) as next:

message.setRecipients(
    Message.RecipientType.TO, 
    new Address[]{new InternetAddress("abc@xyz.com"), new InternetAddress("def@xyz.com")}
);
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • I tried this - `Address[] mail_To = new Address[] {new InternetAddress("abc@xyz.com"), new InternetAddress("efg@xyz.com")}; message.setRecipients(Message.RecipientType.TO, mail_To);` It didn't work. I am receiving individual mails, can't see who all are in the list – Bishwarup Das Sep 08 '16 at 12:13
  • I use the exact same code on my project, and I see properly all the addresses in the TO' section, you problem is elsewhere maybe it is related to your SMTP server? – Nicolas Filotto Sep 08 '16 at 12:25
  • I am using mandrillapp.com, and also i tried using gmail user, still the same – Bishwarup Das Sep 08 '16 at 12:36
  • I use gmail too in my project and I send emails to multiple recipients with this code and I see all the recipients in the email received so I don't know what to add – Nicolas Filotto Sep 08 '16 at 12:39