2015-06-07 97 views
1

我想使用Greenmail來模擬imaps服務器。 我想使用服務器進行系統測試,我想運行綠色郵件服務器併發送郵件,然後從我的應用服務器上運行的作業中獲取這些郵件。 我的問題是GreenMail.start和將GreenMail部署爲web應用程序有什麼不同。 GreenMail.start部署了偵聽的服務器,我可以從不同的機器發送imaps請求嗎?模擬imaps服務器 - Greenmail

我想使用GreenMail.start而不是將GreenMail部署爲webapp的原因是我必須在每次測試運行時在greenmail服務器上創建一個電子郵件帳戶,這是因爲測試將運行在不同的計算機上同時,所以我不想爲所有機器使用相同的帳戶。

我的代碼:

import static org.hamcrest.Matchers.equalTo; 
import static org.junit.Assert.assertEquals; 
import static org.junit.Assert.assertNotNull; 
import static org.junit.Assert.assertThat; 
import static org.junit.Assert.assertTrue; 

import java.io.IOException; 
import java.security.Security; 
import java.util.Properties; 

import javax.mail.Folder; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Store; 
import javax.mail.URLName; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

import com.icegreen.greenmail.util.DummySSLSocketFactory; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 

import com.icegreen.greenmail.user.GreenMailUser; 
import com.icegreen.greenmail.user.UserException; 
import com.icegreen.greenmail.util.GreenMail; 
import com.icegreen.greenmail.util.ServerSetupTest; 
import testIMAPS.MailSender; 

public class GreenmailTest { 
public static final String USER_PASSWORD = "abcdef123"; 
public static final String USER_NAME = "hascode"; 
public static final String EMAIL_USER_ADDRESS = "[email protected]"; 
public static final String EMAIL_TO = "[email protected]"; 
public static final String EMAIL_SUBJECT = "Test E-Mail"; 
public static final String EMAIL_TEXT = "This is a test e-mail."; 
public static final String LOCALHOST = "127.0.0.1"; 
public static GreenMail mailServer; 

@Before 
public void setUp() { 

    if (mailServer == null){ 
     Security.setProperty("ssl.SocketFactory.provider", 
       DummySSLSocketFactory.class.getName()); 
     mailServer = new GreenMail(ServerSetupTest.IMAPS); 
     mailServer.start(); 
    } 
} 

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

@Test 
public void getMails() throws IOException, MessagingException, 
     UserException, InterruptedException { 
    // 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); 

    // fetch the e-mail via imaps using javax.mail .. 
    /*i want this code to be run on different machines to this greenmail server */ 
    Properties props = new Properties(); 
    String imapsProtocol = "imaps"; 
    props.setProperty("mail.store.protocol", imapsProtocol); 

    props.setProperty("mail.imaps.port", String.valueOf(ServerSetupTest.IMAPS.getPort())); 
    Session session = Session.getInstance(props); 
    Store store = session.getStore(imapsProtocol); 
    store.connect(LOCALHOST, USER_NAME, USER_PASSWORD); 

    Folder folder = store.getFolder("INBOX"); 
    folder.open(Folder.READ_ONLY); 
    Message[] messages = folder.getMessages(); 
    assertNotNull(messages); 
    assertThat(1, equalTo(messages.length)); 
    assertEquals(EMAIL_SUBJECT, messages[0].getSubject()); 
    assertTrue(String.valueOf(messages[0].getContent()) 
      .contains(EMAIL_TEXT)); 
    assertEquals(EMAIL_TO, messages[0].getFrom()[0].toString()); 
} 
} 

當我部署在Tomcat上我看到預期我所定義的端口用於Web應用綠票訛詐。但是當我使用mailServer.start()我沒有看到端口在使用中。爲什麼?

謝謝:)

回答

0

是,GreenMail.start開始使用提供ServerSetup配置的服務器。根據主機設置(本地主機與例如0.0.0.0),可以從其他主機訪問GreenMail。無論使用GreenMail.start還是部署GreenMail Web應用程序(它只是一個將其引入應用程序服務器的包裝程序),都可以執行此操作。

您可以預先配置GreenMail Webapp與您需要的用戶。有關如何通過打包的web.xml添加用戶或配置IMAPS端口,請參見configuring GreenMail webapp

關於「看到使用的端口」: 對於mailServer.start(),您可以通過

greenMail.getImaps().getPort()

訪問IMAPS端口當運行/部署訛詐爲Web應用程序,預配置的web.xml文件內的端口。當部署GreenMail Webapp時,它會記錄端口。我認爲這是你「看到使用的端口」的意思?