1

imaps サーバーをシミュレートするために Greenmail使用したいと考えています。サーバーをシステム テストに使用したい。greenmail サーバーを実行してメールを配信し、アプリ サーバーで実行されるジョブからメールを取得したい。私の質問は、GreenMail.start と GreenMail を Web アプリケーションとして展開することの違いは何ですか。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() を使用すると、ポートが使用されていることがわかりません。なぜ?

ありがとう :)

4

1 に答える 1

0

はい、GreenMail.start は提供された ServerSetup 構成を使用してサーバーを起動します。GreenMail は、ホストの設定 (localhost と 0.0.0.0 など) に応じて、他のホストからアクセスできます。これは、GreenMail.start を使用する場合でも、GreenMail webapp をデプロイする場合でも実行できます (アプリケーション サーバーにスローするための単なるラッパーです)。

必要なユーザーで GreenMail Webapp を事前構成できます。ユーザーを追加する方法、またはパッケージ化された web.xml を介して IMAPS ポートを構成する方法については、GreenMail webapp の構成を参照してください。

「使用されているポートの表示」について: mailServer.start() の場合、次の方法で IMAPS ポートにアクセスできます。

greenMail.getImaps().getPort()

GreenMail を webapp として実行/展開する場合、 web.xml 内でポートを事前に構成します。GreenMail Webapp がデプロイされると、ポートがログに記録されます。これが「使用されているポートを見る」という意味だと思いますか?

于 2015-06-21T20:03:56.687 に答える