java mail 多个收件人,在Java中将邮件发送给多个收件人

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

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

OR

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

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

OR

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

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.

解决方案

If you invoke addRecipient multiple times it will add the given recipient to the list of recipients of the given time (TO, CC, BCC)

For example:

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

Will add the 3 addresses to CC

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

You can also use InternetAddress.parse to parse a list of addresses

message.addRecipients(Message.RecipientType.CC,

InternetAddress.parse("abc@abc.com,abc@def.com,ghi@abc.com"));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值