1

I am trying to send multiple mails using JAVA Mail -

When I add a single Recipient -

message.addRecipient(Message.RecipientType.TO, new InternetAddress(“[email protected]”));

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

Here is the code

message.addRecipient(Message.RecipientType.TO, new InternetAddress(“[email protected]”));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(“[email protected]"));

message.addRecipient(Message.RecipientType.CC, new InternetAddress(“[email protected]"));
message.addRecipient(Message.RecipientType.CC, new InternetAddress(“[email protected]"));

message.addRecipient(Message.RecipientType.BCC, new InternetAddress(“[email protected]"));

The mail is sent and received, but when I check the email of [email protected] I can't see that the email has also been sent to [email protected] or vise versa. Neither I can see CC in the list.

Mail details from [email protected]

from:   [email protected]
to: [email protected]
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test

Mail details from [email protected]

from:   [email protected]
to: [email protected]
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test

Mail details from [email protected]

from:   [email protected]
to: [email protected]
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test

Mail details from [email protected]

from:   [email protected]
to: [email protected]
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(“[email protected], [email protected]"));

message.addRecipient(Message.RecipientType.CC, InternetAddress.parse(“[email protected], [email protected]”));

message.addRecipient(Message.RecipientType.BCC, InternetAddress.parse(“[email protected]"));

I am expecting to see the details as -

from:   [email protected]
to: [email protected], [email protected]
cc: [email protected], [email protected]
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("[email protected]"),
                               InternetAddress.parse("[email protected]")};
message.addRecipients(Message.RecipientType.TO, toArray);
Martijn Burger
  • 7,315
  • 8
  • 54
  • 94
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("[email protected]"), new InternetAddress("[email protected]")}
);
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • I tried this - `Address[] mail_To = new Address[] {new InternetAddress("[email protected]"), new InternetAddress("[email protected]")}; 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