模拟imaps服务器 - Greenmail

时间:2015-06-07 18:47:37

标签: email-integration greenmail

我想使用Greenmail来模拟imaps服务器。 我想使用服务器进行系统测试,我想运行greenmail服务器并发送电子邮件,然后从我的应用服务器上运行的作业中获取这些电子邮件。 我的问题是GreenMail.start与将GreenMail部署为webapp之间的区别是什么。 GreenMail.start部署一个侦听服务器,我可以从不同的机器发送imaps请求吗?

我想使用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 = "hascode@localhost";
public static final String EMAIL_TO = "someone@localhost.com";
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上部署greenmail webapp时,我看到了我预期使用的端口。但是当我使用mailServer.start()时,我看不到端口正在使用中。为什么呢?

谢谢:)

1 个答案:

答案 0 :(得分:0)

是的,GreenMail.start使用提供的ServerSetup配置启动服务器。可以从其他主机访问GreenMail,具体取决于主机设置(localhost与例如0.0.0.0)。无论是使用GreenMail.start还是部署GreenMail webapp(它只是将其投入应用程序服务器的包装器),您都可以这样做。  

您可以使用所需的用户预配置GreenMail Webapp。有关如何通过打包的web.xml添加用户或配置IMAPS端口,请参阅configuring GreenMail webapp

关于“看到使用的端口”: 对于mailServer.start(),您可以通过

greenMail.getImaps().getPort()

访问IMAPS端口

在运行/部署GreenMail作为webapp时,您可以预先配置web.xml中的端口。当GreenMail Webapp部署时,它会记录端口。我认为这是“看到端口使用”的意思吗?