2

我正在尝试使用 GreenMail 作为样机邮件服务器进行单元测试。它的工作是等待 IMAP/POP3 请求并发送虚拟邮件,但我无法让它工作。

public class EMailMonitoringTest {
private static final String USER_PASSWORD = "test";
private static final String USER_NAME = "jDownloaderTestUser";
private static final String EMAIL_USER_ADDRESS = "jdtest@localhost";
private static final String EMAIL_TO = "someone@localhost.com";
private static final String EMAIL_SUBJECT = "Test E-Mail";
private static final String EMAIL_TEXT = "This is a test e-mail.";
private static final String LOCALHOST = "127.0.0.1";
private GreenMail mailServer;

@Before
public void setUp() {
    mailServer = new GreenMail(ServerSetupTest.ALL);
    mailServer.start();
}

@After
public void tearDown() {
    mailServer.stop();
}

@Test
public void getMails() throws AddressException, MessagingException, FolderException, UserException, IOException {
    // create user on mail server
    GreenMailUser user = mailServer.setUser(EMAIL_USER_ADDRESS, USER_NAME, USER_PASSWORD);

    // create an e-mail message using javax.mail ..
    MimeMessage message = new MimeMessage((Session) null);
    message.setFrom(new InternetAddress(EMAIL_TO));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(EMAIL_USER_ADDRESS));
    message.setSubject(EMAIL_SUBJECT);
    message.setText(EMAIL_TEXT);

    // use greenmail to store the message
    user.deliver(message);

    // check received messages
    Message[] messages = mailServer.getReceivedMessages();
    for (Message m : messages) {
        System.out.println("*** Class: " + m.getClass() + " ***");
        System.out.println("From: " + m.getFrom()[0]);
        System.out.println("To: " + m.getRecipients(Message.RecipientType.TO)[0]);
        System.out.println("Subject: " + m.getSubject());
        System.out.println("Content: " + m.getContent());
    }

这里没问题,到目前为止的输出是这样的:

** 类:类 javax.mail.internet.MimeMessage **

来自:某人@localhost.com

收件人:jdtest@localhost

主题:测试电子邮件

内容:这是一封测试邮件。

之后,我尝试从 GreenMail IMAP 服务器获取邮件:

    // fetch the e-mail via imap using javax.mail ..
    Properties props = new Properties();
    Session session = Session.getInstance(props);
    URLName urlName = new URLName("imap", LOCALHOST,
    ServerSetupTest.IMAP.getPort(), null, user.getLogin(),
    user.getPassword());
    Store store = session.getStore(urlName);
    store.connect();

    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);
    messages = folder.getMessages();
    System.out.println("Size of Messages: " + messages.length);
    for (Message m : messages) {
        System.out.println("** Class: " + m.getClass() + " **");
        System.out.println("From: " + m.getFrom()[0].toString());
        System.out.println("To: " + m.getRecipients(Message.RecipientType.TO)[0].toString());
        System.out.println("Subject: " + m.getSubject().toString());
        System.out.println("Content: " + m.getContent().toString());
    }

我的奖励是

消息大小:1

** 类:类 com.sun.mail.imap.IMAPMessage **

和一个 FolderClosedException:

javax.mail.FolderClosedException:* BYE JavaMail 异常:com.sun.mail.imap.IMAPMessage.loadEnvelope(IMAPMessage.java:1217) 处 com.sun.mail.imap.IMAPMessage.getFrom(IMAPMessage. java:252) 在 jd.controlling.EMailMonitoringTest.test(EMailMonitoringTest.java:85) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect .DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org .junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在 org.junit.runners.model.FrameworkMethod。在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 调用Explosively(FrameworkMethod.java:41)在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 在 org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner .java:71) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 在 org.junit.runners.ParentRunner$1。在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:236) 在 org.eclipse.jdt.internal.junit4。 runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner。 runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner. java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)java:236) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse .jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)java:236) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse .jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner. java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner. java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

尝试访问 folder.getMessage(0) 只会导致 IndexOutOfBoundsException,但在数组中存储了一个 IMAPMessage 类的消息。有人知道这件事吗?

啊,是的,我尝试了hashCode上的示例,但它们也因同样的原因而失败......访问接收到的 IMAP 消息的属性时出现 FolderClosedException......

感激地接受任何提示。

4

1 回答 1

1

使用 IMAP 时,您配置了 LOCALHOST,但在启动/创建 GreenMail 时从未告诉 GreenMail 使用 LOCALHOST。

使用 GreenMail 服务器ServerSetupTest.IMAP.getBindAddress()的绑定地址而不是LOCALHOST

// fetch the e-mail via imap using javax.mail ..
// Hint: Create session via GreenMailUtil
Session session = GreenMailUtil.getSession(ServerSetupTest.IMAP);
// Use configured host address instead of supplying a 
URLName urlName = new URLName("imap", ServerSetupTest.IMAP.getBindAddress(),
   ServerSetupTest.IMAP.getPort(), null, user.getLogin(),
   user.getPassword());
Store store = session.getStore(urlName);
store.connect();`
于 2014-10-07T08:20:29.743 回答