首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GreenMail不返回密件抄送收件人地址

GreenMail不返回密件抄送收件人地址
EN

Stack Overflow用户
提问于 2017-10-25 19:31:07
回答 1查看 796关注 0票数 3

我在一个测试中使用GreenMail来验证我们的应用程序是否正确地发送电子邮件。

我从GreenMail开始:

代码语言:javascript
复制
GreenMail greenMailServer = new GreenMail(new ServerSetup(port, "localhost", ServerSetup.PROTOCOL_SMTP));
greenMailServer.start();

我有一个单独的进程将电子邮件发送到GreenMail SMTP服务器(这是集成测试的一部分),所以我的测试代码将使用以下内容等待电子邮件:

代码语言:javascript
复制
long endTime = System.currentTimeMillis() + timeout;
// Get the fake mail server and listen for messages
GreenMail mailServer = ITester.getFakeEMail();
while(System.currentTimeMillis() < endTime)  {
     boolean timedOut = !mailServer.waitForIncomingEmail(endTime - System.currentTimeMillis(), 1);
     if(timedOut) {
          throw new TestFailure(lineNumber, "Timed out waiting for email To: '"+to+"' Subject: '"+subject+"' Body: '"+body+"' .");
     }
     for(MimeMessage message : mailServer.getReceivedMessages()) {
          try {
              StringBuilder errors = new StringBuilder();
              // Check who the message is going to
              //Address[] allRecipients = message.getRecipients(Message.RecipientType.BCC);
              Address[] allRecipients = message.getAllRecipients();

我已经尝试了对收件人和getAllRecipients的注释删除请求,但我总是收到null。我已经证实,我的应用程序是发送电子邮件到一个地址在密件抄送字段。

知道我为什么没看到收件人的电子邮件地址吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-25 23:05:55

我在这个博客上找到了答案:

https://developer.vz.net/2011/11/08/unit-testing-java-mail-code/

简短的版本是使用用户并从他的收件箱中获取消息,而不是从整个服务器获取消息。然后你就知道是到了那个电子邮件地址。

代码语言:javascript
复制
GreenMailUser user = mailServer.setUser("junk@somewhere.com", "");
MailFolder inbox = mailServer.getManagers().getImapHostManager().getInbox(user);
for(StoredMessage message : inbox.getMessages()) {
   // Verify the messages
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46940674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档