如何在java中向多个地址发送电子邮件

时间:2014-08-17 05:56:40

标签: java gmail

我使用此方法向两个Gmail ID发送电子邮件,但这会产生错误:

  

Message类型中的方法addRecipient(Message.RecipientType,Address)不适用于参数(Message.RecipientType,Address [])

如何向多个ID发送电子邮件?

 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);

2 个答案:

答案 0 :(得分:2)

如何使用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);

答案 1 :(得分:0)

尝试将代码更改为:

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]);

addRecipient方法不会将数组作为参数,因此您不能一次将整个数组传递给它,但假设它的行为与其名称相符,您应该能够遍历数组并调用addRecipient表示数组中的每个地址。​​