Mirza Ghalib
Mirza Ghalib

Reputation: 31

how to send email to multiple addresses in java

I am using this method to send email to two Gmail ids, but this is giving an error that:

The method addRecipient(Message.RecipientType, Address) in the type Message is not applicable for the arguments (Message.RecipientType, Address[])

How can I send email to multiple ids?

 Address toaddress[] = new InternetAddress[2];
 toaddress[0] = new InternetAddress(mail_to_0);
 toaddress[1] = new InternetAddress(mail_to_1);
 message.addRecipient(Message.RecipientType.TO,toaddress);

Upvotes: 0

Views: 734

Answers (2)

Jayanth
Jayanth

Reputation: 329

how about using addRecipients(Message.RecipientType type,Address[] addresses)

 Address toaddress[] = new InternetAddress[2];
 toaddress[0] = new InternetAddress(mail_to_0);
 toaddress[1] = new InternetAddress(mail_to_1);
 message.addRecipients(Message.RecipientType.TO,toaddress);

Upvotes: 2

Nick Meyer
Nick Meyer

Reputation: 1792

Try changing your code to:

Address toaddress[] = new InternetAddress[2];
toaddress[0] = new InternetAddress(mail_to_0);
toaddress[1] = new InternetAddress(mail_to_1);
for (int i = 0; i < toaddress.length; i++)
    message.addRecipient(Message.RecipientType.TO,toaddress[i]);

The addRecipient method does not take an array as an argument, so you cannot pass it the whole array at once, but assuming it behaves as its name implies, you should be able to loop over the array and call addRecipient for each address in the array.

Upvotes: 0

Related Questions