简体   繁体   English

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

[英]GreenMail not returning BCC recipient addresses

I'm using GreenMail in a test to verify that our application is sending email correctly. 我在测试中使用GreenMail来验证我们的应用程序是否正确发送了电子邮件。

I start GreenMail with: 我以以下方式启动GreenMail:

GreenMail greenMailServer = new GreenMail(new ServerSetup(port, "localhost", ServerSetup.PROTOCOL_SMTP));
greenMailServer.start();

I have a separate process that sends the email to the GreenMail SMTP server (this is part of an integration test), so my test code waits for the email with: 我有一个单独的过程将电子邮件发送到GreenMail SMTP服务器(这是集成测试的一部分),因此我的测试代码以以下方式等待电子邮件:

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

I've tried both the commented out request for Recipients and the getAllRecipients, but I always receive null. 我已经尝试了对收件人和getAllRecipients的注释请求,但是我总是收到null。 I've verified that my application is sending the email to one address in the BCC field. 我已验证我的应用程序正在将电子邮件发送到“密件抄送”字段中的一个地址。

Any idea why I'm not seeing the recipient email address? 知道为什么我看不到收件人的电子邮件地址吗?

I found the answer on this blog: 我在此博客上找到了答案:

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

The short version is to use a user and get the message from his inbox instead of getting the message from the server as a whole. 简短版本是使用用户并从其收件箱中获取消息,而不是从整个服务器中获取消息。 Then you know it came to that email address. 然后,您知道它到了该电子邮件地址。

GreenMailUser user = mailServer.setUser("junk@somewhere.com", "");
MailFolder inbox = mailServer.getManagers().getImapHostManager().getInbox(user);
for(StoredMessage message : inbox.getMessages()) {
   // Verify the messages
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM