将邮件发送给java中的多个收件人

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (24)

我想使用以下方法将消息发送给多个收件人::

message.addRecipient(Message.RecipientType.TO, String arg1);

要么

message.setRecipients(Message.RecipientType.TO,String arg1);

但有一个困惑是,在第二个论证中,如何传递多个地址,如:

message.addRecipient(Message.RecipientType.CC, "abc@abc.com,abc@def.com,ghi@abc.com");

要么

message.addRecipient(Message.RecipientType.CC, "abc@abc.com;abc@def.com;ghi@abc.com");

我也可以使用替代方法发送消息,但想知道上述方法的用途。如果我不能使用它(直到现在我还没有得到任何回答以上要求),那么这种方法需要在邮件API中。

提问于
用户回答回答于

每一个这个代码工作对我来说,请尝试与此发送邮件给多个recepients

private String recipient = "yamabs@gmail.com ,priya@gmail.com ";
String[] recipientList = recipient.split(",");
InternetAddress[] recipientAddress = new InternetAddress[recipientList.length];
int counter = 0;
for (String recipient : recipientList) {
    recipientAddress[counter] = new InternetAddress(recipient.trim());
    counter++;
}
message.setRecipients(Message.RecipientType.TO, recipientAddress);
用户回答回答于

如果您addRecipient多次调用,它会将给定的收件人添加到给定时间的收件人列表(TO,CC,BCC)

例如:

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

将3个地址添加到CC

如果您希望一次添加所有地址,则应使用setRecipientsaddRecipients提供一组地址

Address[] cc = new Address[] {InternetAddress.parse("abc@abc.com"),
                               InternetAddress.parse("abc@def.com"), 
                               InternetAddress.parse("ghi@abc.com")};
message.addRecipients(Message.RecipientType.CC, cc);

您也可以使用InternetAddress.parse解析地址列表

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

扫码关注云+社区