com.icegreen.greenmail.util.ServerSetup Java Examples

The following examples show how to use com.icegreen.greenmail.util.ServerSetup. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GreenMailStartStopListener.java    From syncope with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(final ServletContextEvent sce) {
    ServletContext sc = sce.getServletContext();
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);

    InterruptableGreenMail greenMail = (InterruptableGreenMail) sc.getAttribute(GREENMAIL);
    if (greenMail == null) {
        ServerSetup[] config = new ServerSetup[2];
        config[0] = new ServerSetup(
                ctx.getEnvironment().getProperty("testmail.smtpport", Integer.class),
                "localhost", ServerSetup.PROTOCOL_SMTP);
        config[1] = new ServerSetup(
                ctx.getEnvironment().getProperty("testmail.pop3port", Integer.class),
                "localhost", ServerSetup.PROTOCOL_POP3);
        greenMail = new InterruptableGreenMail(config);
        greenMail.start();

        sc.setAttribute(GREENMAIL, greenMail);
    }

    LOG.info("SMTP and POP3 servers successfully (re)started");
}
 
Example #2
Source File: SshdShellAutoConfigurationTest.java    From sshd-shell-spring-boot with Apache License 2.0 6 votes vote down vote up
@Test
public void testHealthInfoCommand() {
    int smtpPort = SocketUtils.findAvailableTcpPort();
    ServerSetup setup = new ServerSetup(smtpPort, null, ServerSetup.PROTOCOL_SMTP);
    setup.setServerStartupTimeout(5000);
    GreenMail mailServer = new GreenMail(setup);
    JavaMailSenderImpl jmsi = (JavaMailSenderImpl) mailSender;
    mailServer.setUser(jmsi.getUsername(), jmsi.getPassword());
    mailServer.start();
    jmsi.setPort(smtpPort);
    sshCallShell((is, os) -> {
        write(os, "health info");
        verifyResponseContains(is, "{\r\n  \"status\" : \"UP\"");
        mailServer.stop();
    });
}
 
Example #3
Source File: UserManagerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteUserShouldDeleteMail() throws Exception {
    ImapHostManager imapHostManager = new ImapHostManagerImpl(new InMemoryStore());
    UserManager userManager = new UserManager(imapHostManager);

    GreenMailUser user = userManager.createUser("[email protected]", "foo", "pwd");
    assertEquals(1, userManager.listUser().size());

    imapHostManager.createPrivateMailAccount(user);
    MailFolder otherfolder = imapHostManager.createMailbox(user, "otherfolder");
    MailFolder inbox = imapHostManager.getFolder(user, ImapConstants.INBOX_NAME);

    ServerSetup ss = ServerSetupTest.IMAP;
    MimeMessage m1 = GreenMailUtil.createTextEmail("there@localhost", "here@localhost", "sub1", "msg1", ss);
    MimeMessage m2 = GreenMailUtil.createTextEmail("there@localhost", "here@localhost", "sub1", "msg1", ss);

    inbox.store(m1);
    otherfolder.store(m2);
 
    userManager.deleteUser(user);
    assertTrue(imapHostManager.getAllMessages().isEmpty());
}
 
Example #4
Source File: TestEmailNotifier.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  ServerSetup serverSetup = new ServerSetup(getFreePort(), "localhost", "smtp");
  server = new GreenMail(serverSetup);
  server.setUser("user@x", "user", "password");
  server.start();

  Configuration conf = new Configuration();
  conf.set("mail.smtp.host", "localhost");
  conf.set("mail.smtp.port", Integer.toString(server.getSmtp().getPort()));
  emailSender = new EmailSender(conf);

  runtimeInfo = new StandaloneRuntimeInfo(
      RuntimeInfo.SDC_PRODUCT,
      RuntimeModule.SDC_PROPERTY_PREFIX,
      new MetricRegistry(),
      Arrays.asList(TestEmailNotifier.class.getClassLoader())
  );
}
 
Example #5
Source File: GreenMailBean.java    From greenmail with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the server setup, depending on the protocol flags.
 *
 * @return the configured server setups.
 */
private ServerSetup[] createServerSetup() {
    List<ServerSetup> setups = new ArrayList<>();
    if (smtpProtocol) {
        smtpServerSetup = createTestServerSetup(ServerSetup.SMTP);
        setups.add(smtpServerSetup);
    }
    if (smtpsProtocol) {
        smtpsServerSetup = createTestServerSetup(ServerSetup.SMTPS);
        setups.add(smtpsServerSetup);
    }
    if (pop3Protocol) {
        setups.add(createTestServerSetup(ServerSetup.POP3));
    }
    if (pop3sProtocol) {
        setups.add(createTestServerSetup(ServerSetup.POP3S));
    }
    if (imapProtocol) {
        setups.add(createTestServerSetup(ServerSetup.IMAP));
    }
    if (imapsProtocol) {
        setups.add(createTestServerSetup(ServerSetup.IMAPS));
    }
    return setups.toArray(new ServerSetup[setups.size()]);
}
 
Example #6
Source File: DefaultEmailServiceContextBasedTest.java    From spring-boot-email-tools with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    when(templateService.mergeTemplateIntoString(anyString(), any(Map.class)))
            .thenReturn("<!doctype html>\n" +
                    "<html>\n" +
                    "<body>\n" +
                    "<p>\n" +
                    "    THIS IS A TEST WITH TEMPLATE\n" +
                    "</p>\n" +
                    "</body>\n" +
                    "</html>");

    ServerSetup serverSetup = new ServerSetup(MAIL_PORT, (String) null, "smtp");
    testSmtp = new GreenMail(serverSetup);
    testSmtp.start();

    //don't forget to set the test port!
    ((JavaMailSenderImpl) javaMailSender).setPort(serverSetup.getPort());
    ((JavaMailSenderImpl) javaMailSender).setHost("localhost");
}
 
Example #7
Source File: SmtpOutputOperatorTest.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception
{
  node = new SmtpOutputOperator();
  greenMail = new GreenMail(ServerSetupTest.ALL);
  greenMail.start();
  node.setFrom(from);
  node.setContent(content);
  node.setSmtpHost("127.0.0.1");
  node.setSmtpPort(ServerSetupTest.getPortOffset() + ServerSetup.SMTP.getPort());
  node.setSmtpUserName(from);
  node.setSmtpPassword("<password>");
  //node.setUseSsl(true);
  node.setSubject(subject);
  data = new HashMap<String, String>();
  data.put("alertkey", "alertvalue");

}
 
Example #8
Source File: MailServer.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    ServerSetup setup = new ServerSetup(3025, "localhost", "smtp");

    GreenMail greenMail = new GreenMail(setup);
    greenMail.start();

    System.out.println("Started mail server (localhost:3025)");
    System.out.println();
    
    while (true) {
        int c = greenMail.getReceivedMessages().length;

        if (greenMail.waitForIncomingEmail(Long.MAX_VALUE, c + 1)) {
            MimeMessage message = greenMail.getReceivedMessages()[c++];
            System.out.println("-------------------------------------------------------");
            System.out.println("Received mail to " + message.getRecipients(RecipientType.TO)[0]);
            if (message.getContent() instanceof MimeMultipart) {
                MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
                for (int i = 0; i < mimeMultipart.getCount(); i++) {
                    System.out.println("----");
                    System.out.println(mimeMultipart.getBodyPart(i).getContentType() + ":");
                    System.out.println();
                    System.out.println(mimeMultipart.getBodyPart(i).getContent());
                }
            } else {
                System.out.println();
                System.out.println(message.getContent());
            }
            System.out.println("-------------------------------------------------------");
        }
    }
}
 
Example #9
Source File: AbstractServer.java    From greenmail with Apache License 2.0 5 votes vote down vote up
protected AbstractServer(ServerSetup setup, Managers managers) {
    this.setup = setup;
    String bindAddress = setup.getBindAddress();
    if (null == bindAddress) {
        bindAddress = setup.getDefaultBindAddress();
    }
    setName(setup.getProtocol() + ':' + bindAddress + ':' + setup.getPort());
    try {
        bindTo = InetAddress.getByName(bindAddress);
    } catch (UnknownHostException e) {
        throw new RuntimeException("Failed to setup bind address for " + getName(), e);
    }
    this.managers = managers;
}
 
Example #10
Source File: TestEmailSender.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  ServerSetup serverSetup = new ServerSetup(getFreePort(), "localhost", "smtp");
  server = new GreenMail(serverSetup);
  server.setUser("user@x", "user", "password");
  server.start();
}
 
Example #11
Source File: GreenMailStandaloneRunner.java    From greenmail with Apache License 2.0 5 votes vote down vote up
/**
 * Start and configure GreenMail using given properties.
 *
 * @param properties the properties such as System.getProperties()
 */
public void doRun(Properties properties) {
    ServerSetup[] serverSetup = new PropertiesBasedServerSetupBuilder().build(properties);

    if (serverSetup.length == 0) {
        printUsage(System.out);

    } else {
        greenMail = new GreenMail(serverSetup);
        log.info("Starting GreenMail standalone v{} using {}",
                BuildInfo.INSTANCE.getProjectVersion(), Arrays.toString(serverSetup));
        greenMail.withConfiguration(new PropertiesBasedGreenMailConfigurationBuilder().build(properties))
                .start();
    }
}
 
Example #12
Source File: MailOperatorFactoryTest.java    From digdag with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void smtpSetup()
{
    ServerSetup servers[] = {ServerSetupTest.SMTP, ServerSetupTest.POP3};
    greenMail = new GreenMail(servers);
    greenMail.start();
    smtpPort = greenMail.getSmtp().getPort();
}
 
Example #13
Source File: SendEmailTestCase.java    From charles-rest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Mock a smtp server.
 * @return GreenMail smtp server.
 * @throws IOException If something goes wrong.
 */
public GreenMail smtpServer(String bind, int port) throws IOException {
    return new GreenMail(
        new ServerSetup(
            port, bind,
            ServerSetup.PROTOCOL_SMTP
        )
    );
}
 
Example #14
Source File: GreenMailBean.java    From greenmail with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test server setup with configured offset.
 *
 * @param theSetup the server setup.
 * @return the test server setup.
 */
private ServerSetup createTestServerSetup(final ServerSetup theSetup) {
    ServerSetup serverSetup =
            new ServerSetup(portOffset + theSetup.getPort(),
                           hostname,
                           theSetup.getProtocol());
    serverSetup.setServerStartupTimeout(serverStartupTimeout);
    return serverSetup;
}
 
Example #15
Source File: DockerServiceIT.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllServices() throws MessagingException, InterruptedException {
    // Ugly workaround : GreenMail in docker starts with open TCP connections,
    //                   but TLS sockets might not be ready yet.
    TimeUnit.SECONDS.sleep(1);

    // Send messages via SMTP and secure SMTPS
    GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost",
            "test1", "Test GreenMail Docker service",
            ServerSetupTest.SMTP.createCopy(bindAddress));
    GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost",
            "test2", "Test GreenMail Docker service",
            ServerSetupTest.SMTPS.createCopy(bindAddress));

    // IMAP
    for (ServerSetup setup : Arrays.asList(
            ServerSetupTest.IMAP.createCopy(bindAddress),
            ServerSetupTest.IMAPS.createCopy(bindAddress),
            ServerSetupTest.POP3.createCopy(bindAddress),
            ServerSetupTest.POP3S.createCopy(bindAddress))) {
        final Store store = Session.getInstance(setup.configureJavaMailSessionProperties(null, false)).getStore();
        store.connect("foo@localhost", "foo@localhost");
        try {
            Folder folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            assertEquals("Can not check mails using "+store.getURLName(), 2, folder.getMessageCount());
        } finally {
            store.close();
        }
    }
}
 
Example #16
Source File: GreenMailRule.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
protected void before() throws Throwable {
    ServerSetup setup = new ServerSetup(port, host, "smtp");

    greenMail = new GreenMail(setup);
    greenMail.start();
}
 
Example #17
Source File: MailServer.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static void start() {
    ServerSetup setup = new ServerSetup(Integer.parseInt(PORT), HOST, "smtp");

    greenMail = new GreenMail(setup);
    greenMail.start();

    log.info("Started mail server (" + HOST + ":" + PORT + ")");
}
 
Example #18
Source File: AllocateAvailablePortTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
private ServerSetup smtpServerAtPort(int port) {
    return new ServerSetup(port, null, ServerSetup.PROTOCOL_SMTP);
}
 
Example #19
Source File: Pop3Server.java    From greenmail with Apache License 2.0 4 votes vote down vote up
public Pop3Server(ServerSetup setup, Managers managers) {
    super(setup, managers);
}
 
Example #20
Source File: AbstractServer.java    From greenmail with Apache License 2.0 4 votes vote down vote up
public ServerSetup getServerSetup() {
    return setup;
}
 
Example #21
Source File: AlfrescoImapServer.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public DefaultImapServer(ServerSetup setup, Managers managers, AtomicReference<Exception> serverOpeningExceptionRef)
{
    super(setup, managers, serverOpeningExceptionRef);
}
 
Example #22
Source File: ImapServer.java    From greenmail with Apache License 2.0 4 votes vote down vote up
public ImapServer(ServerSetup setup, Managers managers) {
    super(setup, managers);
}
 
Example #23
Source File: SmtpServer.java    From greenmail with Apache License 2.0 4 votes vote down vote up
public SmtpServer(ServerSetup setup, Managers managers) {
    super(setup, managers);
}
 
Example #24
Source File: InterruptableSmtpServer.java    From syncope with Apache License 2.0 4 votes vote down vote up
public InterruptableSmtpServer(final ServerSetup setup, final Managers managers) {
    super(setup, managers);
}
 
Example #25
Source File: SmtpMock.java    From mangooio with Apache License 2.0 4 votes vote down vote up
public void start() {
    if (Application.inDevMode() || Application.inTestMode()) {
        this.greenMail = new GreenMail(new ServerSetup(this.config.getSmtpPort(), this.config.getSmtpHost(), Default.SMTP_SERVER_NAME.toString()));
        this.greenMail.start();
    }
}
 
Example #26
Source File: CommonTest.java    From hawkular-alerts with Apache License 2.0 4 votes vote down vote up
@Before
public void initSmtpServer() {
    server = new GreenMail(new ServerSetup(TEST_SMTP_PORT, TEST_SMTP_HOST, "smtp"));
    server.start();
}
 
Example #27
Source File: SmtpServerTest.java    From hawkular-alerts with Apache License 2.0 4 votes vote down vote up
@Before
public void startSmtpServer() {
    server = new GreenMail(new ServerSetup(TEST_SMTP_PORT, TEST_SMTP_HOST, "smtp"));
    server.start();
}
 
Example #28
Source File: AdvancedEmailServerValidatorTest.java    From robozonky with Apache License 2.0 4 votes vote down vote up
private static ServerSetup getServerSetup() {
    final ServerSetup setup = ServerSetupTest.SMTP;
    setup.setServerStartupTimeout(5000);
    setup.setVerbose(true);
    return setup;
}
 
Example #29
Source File: EmailSettingsValidatorTest.java    From robozonky with Apache License 2.0 4 votes vote down vote up
private static ServerSetup getServerSetup() {
    final ServerSetup setup = ServerSetupTest.SMTP;
    setup.setServerStartupTimeout(5000);
    setup.setVerbose(true);
    return setup;
}
 
Example #30
Source File: SimpleEmailServerValidatorTest.java    From robozonky with Apache License 2.0 4 votes vote down vote up
private static ServerSetup getServerSetup() {
    final ServerSetup setup = ServerSetupTest.SMTP;
    setup.setServerStartupTimeout(5000);
    setup.setVerbose(true);
    return setup;
}