通过javax.mail发送多封电子邮件

时间:2016-09-08 11:27:42

标签: java mailx

我正在尝试使用JAVA Mail发送多封邮件 -

当我添加一个收件人时 -

message.addRecipient(Message.RecipientType.TO, new InternetAddress(“abc@xyz.com”));

它工作正常,但是当我添加多个电子邮件地址时却没有 -

这是代码

message.addRecipient(Message.RecipientType.TO, new InternetAddress(“abc@xyz.com”));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(“def@xyz.com"));

message.addRecipient(Message.RecipientType.CC, new InternetAddress(“ghi@xyz.com"));
message.addRecipient(Message.RecipientType.CC, new InternetAddress(“jkl@xyz.com"));

message.addRecipient(Message.RecipientType.BCC, new InternetAddress(“mno@xyz.com"));

发送和接收邮件,但是当我查看abc@xyz.com的电子邮件时,我发现电子邮件也已发送到def@xyz.com或反之亦然。我也不能在列表中看到CC。

来自abc@xyz.com的邮件详情

from:   xyz@xyz.com
to: abc@xyz.com
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test

来自def@xyz.com的邮件详情

from:   xyz@xyz.com
to: def@xyz.com
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test

来自ghi@xyz.com的邮件详情

from:   xyz@xyz.com
to: ghi@xyz.com
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test

来自jkl@xyz.com的邮件详情

from:   xyz@xyz.com
to: jkl@xyz.com
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test

我尝试稍微更改逻辑,但结果相同 -

message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(“abc@xyz.com, def@xyz.com"));

message.addRecipient(Message.RecipientType.CC, InternetAddress.parse(“ghi@xyz.com, jkl@xyz.com”));

message.addRecipient(Message.RecipientType.BCC, InternetAddress.parse(“mno@xyz.com"));

我希望将详细信息视为 -

from:   xyz@xyz.com
to: abc@xyz.com, def@xyz.com
cc: ghi@xyz.com, jkl@xyz.com
date:   Thu, Sep 8, 2016 at 4:38 PM
subject:    Test

2 个答案:

答案 0 :(得分:1)

你应该尝试:

Address[] toArray = new Address[] {InternetAddress.parse("abc@xyz.com"),
                               InternetAddress.parse("def@xyz.com")};
message.addRecipients(Message.RecipientType.TO, toArray);

答案 1 :(得分:1)

为防止出现任何错误和意外,我建议您使用setRecipients(Message.RecipientType type, Address[] addresses)作为下一个:

message.setRecipients(
    Message.RecipientType.TO, 
    new Address[]{new InternetAddress("abc@xyz.com"), new InternetAddress("def@xyz.com")}
);