@Test
 public void testSmtpServerTimeout() throws Throwable {
   assertEquals(0, greenMail.getReceivedMessages().length);
   long t0 = System.currentTimeMillis();
   greenMail.waitForIncomingEmail(500, 1);
   assertTrue(System.currentTimeMillis() - t0 > 500);
   MimeMessage[] emails = greenMail.getReceivedMessages();
   assertEquals(0, emails.length);
 }
  @Test
  public void testSmtpServerReceiveWithSetup() throws Throwable {
    assertEquals(0, greenMail.getReceivedMessages().length);

    String subject = GreenMailUtil.random();
    String body = GreenMailUtil.random();
    GreenMailUtil.sendTextEmailTest("*****@*****.**", "*****@*****.**", subject, body);
    greenMail.waitForIncomingEmail(1500, 1);
    MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(1, emails.length);
    assertEquals(subject, emails[0].getSubject());
    assertEquals(body, GreenMailUtil.getBody(emails[0]).trim());
  }
  @Test
  public void testLargeMessageBody() throws MessagingException, IOException {
    String to = "to@localhost";
    GreenMailUtil.sendMessageBody(
        to,
        "from@localhost",
        "Subject",
        createLargeByteArray(),
        "application/blubb",
        greenMail.getSmtp().getServerSetup());
    greenMail.waitForIncomingEmail(5000, 1);

    retrieveAndCheckBody(new Retriever(greenMail.getPop3()), to);
    retrieveAndCheckBody(new Retriever(greenMail.getImap()), to);
  }
 @Test
 public void attachment() throws MessageException, MessagingException, IOException {
   sender.send(
       new Email(
           "Subject",
           "Body",
           new EmailAddress("*****@*****.**"),
           "*****@*****.**",
           new Attachment(
               new File(getClass().getResource("/attachment/04-Java-OOP-Basics.pdf").getFile()))));
   AssertEmail.assertEquals(
       new ExpectedEmail("Subject", "Body", "*****@*****.**", "*****@*****.**"),
       greenMail.getReceivedMessages());
   AssertAttachment.assertEquals(
       new ExpectedAttachment("/attachment/04-Java-OOP-Basics.pdf", "application/pdf.*"),
       greenMail.getReceivedMessages());
 }
 @Test
 public void simple() throws MessageException, MessagingException {
   sender.send(
       new Email("Subject", "Body", new EmailAddress("*****@*****.**"), "*****@*****.**"));
   AssertEmail.assertEquals(
       new ExpectedEmail("Subject", "Body", "*****@*****.**", "*****@*****.**"),
       greenMail.getReceivedMessages());
 }
 @Test
 public void testSmtpServerBasic() throws MessagingException {
   GreenMailUtil.sendTextEmailTest("*****@*****.**", "*****@*****.**", "subject", "body");
   MimeMessage[] emails = greenMail.getReceivedMessages();
   assertEquals(1, emails.length);
   assertEquals("subject", emails[0].getSubject());
   assertEquals("body", GreenMailUtil.getBody(emails[0]));
 }
 @Test
 public void testSmtpServerLeadingPeriods() throws MessagingException {
   String body = ". body with leading period";
   GreenMailUtil.sendTextEmailTest("*****@*****.**", "*****@*****.**", "subject", body);
   MimeMessage[] emails = greenMail.getReceivedMessages();
   assertEquals(1, emails.length);
   assertEquals("subject", emails[0].getSubject());
   assertEquals(body, GreenMailUtil.getBody(emails[0]));
 }
 private Message fetchMail(String loginId, String password)
     throws MessagingException, InterruptedException {
   ImapServer imapd = mailMock.getImap();
   Message msg = null;
   while ((msg = retrieveViaImap(imapd.getBindTo(), imapd.getPort(), loginId, password)) == null) {
     MILLISECONDS.sleep(100);
   }
   return msg;
 }
 private void loadRoutesDefinition(
     InputStream resourceAsStream, CamelContext context, String commandName) throws Exception {
   SmtpServer smtpd = mailMock.getSmtp();
   ImapServer imapd = mailMock.getImap();
   Map<String, Object> values =
       MapBuilder.<String, Object>newMapBuilder()
           .put("imaphost", imapd.getBindTo())
           .put("imapport", imapd.getPort())
           .put("commandname", commandName)
           .put("command", "D1=true;A2=123")
           .put("smtphost", smtpd.getBindTo())
           .put("smtpport", smtpd.getPort())
           .build();
   String xml = replacePlaceHolders(toString(resourceAsStream), values);
   InputStream is = new ByteArrayInputStream(xml.getBytes());
   try {
     context.addRouteDefinitions(context.loadRoutesDefinition(is).getRoutes());
   } finally {
     is.close();
   }
 }
  @Test
  public void writesResultToSender() throws Exception {
    final String receiver = "*****@*****.**";
    createMailUser(receiver, "loginIdReceiver", "secretOfReceiver");

    String validSender = "*****@*****.**";
    createMailUser(validSender, "loginIdSender", "secretOfSender");
    sendMailTo(receiver).from(validSender).withSubject(anySubject()).andText("usedScenario");

    final String ardulink =
        makeURI(
            mockURI,
            newMapBuilder()
                .put("validfroms", validSender)
                .put("scenario.usedScenario", "D1=true;A2=123")
                .build());

    SmtpServer smtpd = mailMock.getSmtp();
    final String smtp =
        "smtp://"
            + smtpd.getBindTo()
            + ":"
            + smtpd.getPort()
            + "?username="******"loginIdReceiver"
            + "&password="******"secretOfReceiver"
            + "&debugMode=true";

    CamelContext context = new DefaultCamelContext();
    context.addRoutes(
        new RouteBuilder() {
          @Override
          public void configure() {
            from(localImap(receiver))
                .to(ardulink)
                .setHeader("to", simple("${in.header.from}"))
                .setHeader("from", simple("${in.header.to}"))
                .to(smtp);
          }
        });
    context.start();

    try {
      assertThat(
          ((String) fetchMail("loginIdSender", "secretOfSender").getContent()),
          is(
              "SwitchDigitalPinCommand [pin=1, value=true]=OK\r\n"
                  + "SwitchAnalogPinCommand [pin=2, value=123]=OK"));
    } finally {
      context.stop();
    }
  }
 @Test
 public void emailSentToOneCustomer() throws Exception {
   underTest.sendGreetings(new BirthDate("2016-08-19"));
   MimeMessage[] emails = greenMail.getReceivedMessages();
   assertThat("Number of messages", emails.length, is(1));
   assertMessage(
       emails[0],
       new BirthdayMessage(
           "*****@*****.**",
           "*****@*****.**",
           "Alles Gute zum Geburtstag!",
           "Liebe Anastasia, Zu deinem 38. Geburtstag alles Gute ..."));
 }
  @Test
  public void testSmtpServerReceiveInThread() throws Throwable {
    assertEquals(0, greenMail.getReceivedMessages().length);

    Thread sendThread =
        new Thread() {
          public void run() {
            try {
              Thread.sleep(700);
              GreenMailUtil.sendTextEmailTest(
                  "*****@*****.**", "*****@*****.**", "abc", "def");
            } catch (Throwable e) {
              throw new RuntimeException(e);
            }
          }
        };
    sendThread.start();
    greenMail.waitForIncomingEmail(3000, 1);
    MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(1, emails.length);
    sendThread.join(10000);
  }
  @Test
  public void testSmtpServerReceiveMultipart() throws Exception {
    assertEquals(0, greenMail.getReceivedMessages().length);

    String subject = GreenMailUtil.random();
    String body = GreenMailUtil.random();
    GreenMailUtil.sendAttachmentEmail(
        "*****@*****.**",
        "*****@*****.**",
        subject,
        body,
        new byte[] {0, 1, 2},
        "image/gif",
        "testimage_filename",
        "testimage_description",
        ServerSetupTest.SMTP);
    greenMail.waitForIncomingEmail(1500, 1);
    Message[] emails = greenMail.getReceivedMessages();
    assertEquals(1, emails.length);
    assertEquals(subject, emails[0].getSubject());

    Object o = emails[0].getContent();
    assertTrue(o instanceof MimeMultipart);
    MimeMultipart mp = (MimeMultipart) o;
    assertEquals(2, mp.getCount());
    BodyPart bp;
    bp = mp.getBodyPart(0);
    assertEquals(body, GreenMailUtil.getBody(bp).trim());

    bp = mp.getBodyPart(1);
    assertEquals("AAEC", GreenMailUtil.getBody(bp).trim());

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    GreenMailUtil.copyStream(bp.getInputStream(), bout);
    byte[] gif = bout.toByteArray();
    for (int i = 0; i < gif.length; i++) {
      assertEquals(i, gif[i]);
    }
  }
 private String localImap(String receiver) {
   ImapServer imapd = mailMock.getImap();
   return makeURI(
       "imap://" + receiver,
       newMapBuilder()
           .putAll(IMAP_DEFAULTS)
           .put("username", "loginIdReceiver")
           .put("password", "secretOfReceiver")
           .put("port", imapd.getPort())
           .put("unseen", true)
           .put("delete", true)
           .put("consumer.delay", MINUTES.toMillis(10))
           .build());
 }
 private void waitUntilMailWasFetched() throws InterruptedException {
   while (mailMock.getReceivedMessages().length > 0) {
     MILLISECONDS.sleep(50);
   }
 }
 @Test
 public void emailsForCustomersWithSameBirthday() throws Exception {
   underTest.sendGreetings(new BirthDate("2016-09-23"));
   MimeMessage[] emails = greenMail.getReceivedMessages();
   assertThat("Number of messages", emails.length, is(2));
 }
 private void createMailUser(String receiver, String id, String password) {
   mailMock.setUser(receiver, id, password);
 }