在Java中将邮件发送给多个收件人 [英] Send Mail to multiple Recipients in java

查看:1296
本文介绍了在Java中将邮件发送给多个收件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下方法向多个收件人发送邮件::

I want to send message to multiple Recipients using following method ::

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

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

但有一个混淆是在第二个争论中,
如何传递多个地址,如: / p>

But one confusion is that in Second arguement, How to pass multiple addresses like :

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中。

I can send message using alternate methods too, but want to know the purpose of above method. If i cant use it(as till now i haven't got any answer for above requirement) then what is the need for this method to be in mail API.

推荐答案

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

If you invoke addRecipient multiple times it will add the given recipient to the list of recipients of the given time (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

Will add the 3 addresses to CC

如果您希望一次添加所有地址,您应该使用 setRecipients addRecipients 并为其提供一组地址

If you wish to add all addresses at once you should use setRecipients or addRecipients and provide it with an array of addresses

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






您还可以使用 InternetAddressAddress.parse 解析地址列表

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

这篇关于在Java中将邮件发送给多个收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆