Java Code Examples for com.icegreen.greenmail.util.GreenMail#getReceivedMessages()

The following examples show how to use com.icegreen.greenmail.util.GreenMail#getReceivedMessages() . 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: TestStandaloneRunner.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000)
public void testPipelineStoppedWithMail() throws Exception {
  Runner runner = pipelineManager.getRunner( TestUtil.PIPELINE_WITH_EMAIL, "0");
  runner.start(new StartPipelineContextBuilder("admin").build());
  waitForState(runner, PipelineStatus.RUNNING);
  runner.getRunner(AsyncRunner.class).getDelegatingRunner().prepareForStop("admin");
  runner.getRunner(AsyncRunner.class).getDelegatingRunner().stop("admin");
  waitForState(runner, PipelineStatus.STOPPED);
  //wait for email
  GreenMail mailServer = TestUtil.TestRuntimeModule.getMailServer();
  while(mailServer.getReceivedMessages().length < 1) {
    ThreadUtil.sleep(100);
  }
  String headers = GreenMailUtil.getHeaders(mailServer.getReceivedMessages()[0]);
  Assert.assertTrue(headers.contains("To: foo, bar"));
  Assert.assertTrue(headers.contains("Subject: StreamsSets Data Collector Alert - " +
      TestUtil.PIPELINE_TITLE_WITH_EMAIL + " - STOPPED"));
  Assert.assertTrue(headers.contains("From: sdc@localhost"));
  Assert.assertNotNull(GreenMailUtil.getBody(mailServer.getReceivedMessages()[0]));

  mailServer.reset();
}
 
Example 2
Source File: TestStandaloneRunner.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000)
public void testPipelineFinishWithMail() throws Exception {
  Runner runner = pipelineManager.getRunner( TestUtil.PIPELINE_WITH_EMAIL, "0");
  runner.start(new StartPipelineContextBuilder("admin").build());
  waitForState(runner, PipelineStatus.RUNNING);
  assertNull(runner.getState().getMetrics());
  TestUtil.EMPTY_OFFSET = true;
  waitForState(runner, PipelineStatus.FINISHED);
  assertNotNull(runner.getState().getMetrics());
  //wait for email
  GreenMail mailServer = TestUtil.TestRuntimeModule.getMailServer();
  while(mailServer.getReceivedMessages().length < 1) {
    ThreadUtil.sleep(100);
  }
  String headers = GreenMailUtil.getHeaders(mailServer.getReceivedMessages()[0]);
  Assert.assertTrue(headers.contains("To: foo, bar"));
  Assert.assertTrue(headers.contains("Subject: StreamsSets Data Collector Alert - " +
      TestUtil.PIPELINE_TITLE_WITH_EMAIL + " - FINISHED"));
  Assert.assertTrue(headers.contains("From: sdc@localhost"));
  Assert.assertNotNull(GreenMailUtil.getBody(mailServer.getReceivedMessages()[0]));

  mailServer.reset();
}
 
Example 3
Source File: ConcurrentCloseIT.java    From greenmail with Apache License 2.0 6 votes vote down vote up
private void testThis() throws InterruptedException {
    final GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP);
    greenMail.setUser("test@localhost", "test@localhost");
    greenMail.start();
    final SenderThread sendThread = new SenderThread();
    try {
        sendThread.start();
        greenMail.waitForIncomingEmail(3000, 1);
        final MimeMessage[] emails = greenMail.getReceivedMessages();
        assertEquals(1, emails.length);
        sendThread.join(10000);
    } finally {
        greenMail.stop();
    }

    if (sendThread.exc != null) {
        throw sendThread.exc;
    }
}
 
Example 4
Source File: MailHelper.java    From QuizZz with MIT License 5 votes vote down vote up
public static String waitForEmailAndExtractUrl(GreenMail smtpServer) throws IOException, MessagingException {
	smtpServer.waitForIncomingEmail(1);
	Message[] messages = smtpServer.getReceivedMessages();
	smtpServer.reset();
	assertEquals(1, messages.length);

	return extractUrlFromMail(messages[0]);
}
 
Example 5
Source File: SendEmailTestCase.java    From charles-rest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * SendEmail can send an email envelope to the SMTP server.
 * @throws Exception If something goes wrong.
 */
@Test
public void sendsEmaiLToSmtpServer() throws Exception  {
    String bind = "localhost";
    int port = this.port();
    GreenMail server = this.smtpServer(bind, port);
    server.start();

    SendEmail se = new SendEmail(
        new Postman.Default(
            new SMTP(
                new Token("", "")
                    .access(new Protocol.SMTP(bind, port))
            )
        ), 
        new Envelope.MIME()
            .with(new StSender("Charles Michael <[email protected]>"))
            .with(new StRecipient("[email protected]"))
            .with(new StSubject("test subject: test email"))
            .with(new EnPlain("hello, how are you? This is a test email..."))
    );

    try {
        se.perform(Mockito.mock(Command.class), Mockito.mock(Logger.class));
        final MimeMessage[] messages = server.getReceivedMessages();
        assertTrue(messages.length == 1);
        for (final Message msg : messages) {
            assertTrue(msg.getFrom()[0].toString().contains("<[email protected]>"));
            assertTrue(msg.getSubject().contains("test email"));
        }
    } finally {
        server.stop();
    }
}
 
Example 6
Source File: SendEmailTestCase.java    From charles-rest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * SendEmail can send an email when username, password, host and port are specified.
 * @throws Exception If something goes wrong.
 */
@Test
public void sendsEmailFromGivenUser() throws Exception {
    String bind = "localhost";
    int port = this.port();
    GreenMail server = this.smtpServer(bind, port);
    server.start();
    
    System.setProperty("charles.smtp.username","mihai");
    System.setProperty("charles.smtp.password","");
    System.setProperty("charles.smtp.host","localhost");
    System.setProperty("charles.smtp.port", String.valueOf(port));

    SendEmail se = new SendEmail("[email protected]", "hello", "hello, how are you?");

    try {
        se.perform(Mockito.mock(Command.class), Mockito.mock(Logger.class));
        final MimeMessage[] messages = server.getReceivedMessages();
        assertTrue(messages.length == 1);
        for (final Message msg : messages) {
            assertTrue(msg.getFrom()[0].toString().contains("mihai"));
            assertTrue(msg.getSubject().contains("hello"));
        }
    } finally {
        server.stop();
    }
}
 
Example 7
Source File: ExampleSendNoRuleAdvTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSend() throws MessagingException, IOException {
    GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
    try {
        greenMail.start();

        //Use random content to avoid potential residual lingering problems
        final String subject = GreenMailUtil.random();
        final String body = GreenMailUtil.random();

        sendTestMails(subject, body); // Place your sending code here

        //wait for max 5s for 1 email to arrive
        //waitForIncomingEmail() is useful if you're sending stuff asynchronously in a separate thread
        assertTrue(greenMail.waitForIncomingEmail(5000, 2));

        //Retrieve using GreenMail API
        Message[] messages = greenMail.getReceivedMessages();
        assertEquals(2, messages.length);

        // Simple message
        assertEquals(subject, messages[0].getSubject());
        assertEquals(body, GreenMailUtil.getBody(messages[0]).trim());

        //if you send content as a 2 part multipart...
        assertTrue(messages[1].getContent() instanceof MimeMultipart);
        MimeMultipart mp = (MimeMultipart) messages[1].getContent();
        assertEquals(2, mp.getCount());
        assertEquals("body1", GreenMailUtil.getBody(mp.getBodyPart(0)).trim());
        assertEquals("body2", GreenMailUtil.getBody(mp.getBodyPart(1)).trim());
    } finally {
        greenMail.stop();
    }
}
 
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("-------------------------------------------------------");
        }
    }
}