【问题标题】:Sending multiple email over javax.mail通过 javax.mail 发送多封电子邮件
【发布时间】:2016-09-08 11:27:41
【问题描述】:

我正在尝试使用 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

【问题讨论】:

标签: java mailx


【解决方案1】:

你应该试试:

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

【讨论】:

  • 我试过了 - Address[] mail_To = new Address[] {new InternetAddress("abc@xyz.com"), new InternetAddress("efg@xyz.com")}; message.setRecipients(Message.RecipientType.TO, mail_To); 没用。我正在接收个人邮件,无法查看列表中的所有人
【解决方案2】:

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

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

【讨论】:

  • 我试过了 - Address[] mail_To = new Address[] {new InternetAddress("abc@xyz.com"), new InternetAddress("efg@xyz.com")}; message.setRecipients(Message.RecipientType.TO, mail_To); 没用。我正在接收个人邮件,无法查看列表中的所有人
  • 我在我的项目中使用了完全相同的代码,并且我正确地看到了 TO' 部分中的所有地址,您的问题出在其他地方,也许它与您的 SMTP 服务器有关?
  • 我用的是mandrillapp.com,也试过用gmail用户,还是一样
  • 我在我的项目中也使用 gmail,我使用此代码向多个收件人发送电子邮件,我看到收到的电子邮件中的所有收件人,所以我不知道要添加什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
  • 2012-09-06
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 2016-07-17
  • 1970-01-01
相关资源
最近更新 更多