2013-03-25 92 views
2

我試圖使用GreenMail作爲模型郵件服務器進行單元測試。它的工作是等待IMAP/POP3請求併發送虛擬郵件,但我無法使其工作。無法訪問GreenMail收到的電子郵件的屬性

public class EMailMonitoringTest { 
private static final String USER_PASSWORD = "test"; 
private static final String USER_NAME = "jDownloaderTestUser"; 
private static final String EMAIL_USER_ADDRESS = "[email protected]"; 
private static final String EMAIL_TO = "[email protected]"; 
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 **

來源:有人@本地.COM

要:jdtest @本地

主題:測試電子郵件

內容:這是一封測試電子郵件。

在那之後,我嘗試從訛詐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

**類:class com.sun.mail.imap.IMAPMessage **

和FolderClosedException:

javax.mail.FolderClosedException:* BYE JavaMail的例外: java.io.IOException的在 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(本機方法)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Delegati ngMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:601)在 org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:44) 在 組織。 junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 在 org.junit.internal.runners。 statements.InvokeMethod.evaluate(InvokeMethod.java:20) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 在 org.junit.internal.runners.statements.RunAfters。評估(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) at org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:193)at org.junit.runners.ParentRunner $ 1。日程表(ParentRunner.java:52) org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42)at org.junit .runners.ParentRunner $ 2.evaluate(ParentRunner.java:184)at org.junit.runners.ParentRunner.run(ParentRunner.java:236)at 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(RemoteTes tRunner.java:683) 在 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異常,但存儲在Array類IMAPMessage的一個消息。有人知道這個嗎?

是啊,我訪問收到IMAP郵件的屬性...

感激會接受任何提示,當試圖實例上hashCode,但他們也無法以相同的理由... FolderClosedException。

+0

嘗試遠程登錄,首先驗證您所得到的郵件和您的IMAP服務器工作正常。 – Anshul 2013-03-25 04:31:49

+0

我可以看到新消息,但只要我想獲取它,測試類就會崩潰,但出現以下異常: java.lang.RuntimeException:無法啓動至少一個郵件服務。 \t在com.icegreen.greenmail.util.GreenMail.start(來源不明) \t在jd.controlling.EMailMonitoringTest.setUp(EMailMonitoringTest.java:44) \t在sun.reflect.NativeMethodAccessorImpl.invoke0(本機方法) \t at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ...所以IMAP服務器不能正常工作。 : -/ – RyuX51 2013-03-25 06:36:14

回答

0

使用IMAP時,您配置LOCALHOST,但從未告知GreenMail在啓動/創建GreenMail時使用LOCALHOST。

使用訛詐服務器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();` 
+0

這個問題已經很老了,不再適用於我。如果有人有同樣的問題,試試這個答案,它的工作原理,請告訴我,以便我可以接受這個答案。 :) – RyuX51 2015-03-05 16:43:50

相關問題