Search in sources :

Example 1 with SmtpServer

use of com.icegreen.greenmail.smtp.SmtpServer in project syndesis by syndesisio.

the class EMailTestServer method generateFolder.

public void generateFolder(String user, String password, String folderName) throws Exception {
    if (server instanceof SmtpServer) {
        throw new Exception("SMTP not applicable for generating folders");
    }
    Store store = server.createStore();
    store.connect(user, password);
    Folder newFolder = store.getFolder(folderName);
    if (!newFolder.exists()) {
        newFolder.create(Folder.HOLDS_MESSAGES);
        assertTrue(newFolder.exists());
    }
    newFolder.open(Folder.READ_WRITE);
    assertTrue(newFolder.isOpen());
    List<MimeMessage> msgs = new ArrayList<>();
    for (int i = 1; i <= 5; ++i) {
        // Use random content to avoid potential residual lingering problems
        String subject = folderName + SPACE + HYPHEN + SPACE + GreenMailUtil.random();
        String body = folderName + NEW_LINE + GreenMailUtil.random();
        GreenMailUser greenUser = greenMail.setUser(user, password);
        // Construct message
        msgs.add(createTextMessage(greenUser.getEmail(), "Ben" + i + "@test.com", subject, body));
    }
    newFolder.appendMessages(msgs.toArray(new MimeMessage[0]));
    assertEquals(msgs.size(), newFolder.getMessageCount());
}
Also used : SmtpServer(com.icegreen.greenmail.smtp.SmtpServer) MimeMessage(javax.mail.internet.MimeMessage) ArrayList(java.util.ArrayList) Store(javax.mail.Store) KeyStore(java.security.KeyStore) GreenMailUser(com.icegreen.greenmail.user.GreenMailUser) Folder(javax.mail.Folder) MessagingException(javax.mail.MessagingException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Example 2 with SmtpServer

use of com.icegreen.greenmail.smtp.SmtpServer in project syndesis by syndesisio.

the class EMailTestServer method getEmailCountInFolder.

public int getEmailCountInFolder(String user, String password, String folderName) throws Exception {
    if (server instanceof SmtpServer) {
        throw new Exception("SMTP not applicable for reading folders");
    }
    Store store = server.createStore();
    store.connect(user, password);
    Folder newFolder = store.getFolder(folderName);
    if (!newFolder.exists()) {
        throw new Exception("No folder with name " + folderName);
    }
    newFolder.open(Folder.READ_ONLY);
    return newFolder.getMessageCount();
}
Also used : SmtpServer(com.icegreen.greenmail.smtp.SmtpServer) Store(javax.mail.Store) KeyStore(java.security.KeyStore) Folder(javax.mail.Folder) MessagingException(javax.mail.MessagingException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Example 3 with SmtpServer

use of com.icegreen.greenmail.smtp.SmtpServer in project concord by walmartlabs.

the class SmtpTaskTest method testAttachments.

@Test
public void testAttachments() throws Exception {
    SmtpServer server = mail.getSmtp();
    Map<String, Object> smtpParams = new HashMap<>();
    smtpParams.put("host", "localhost");
    smtpParams.put("port", server.getPort());
    Map<String, Object> mailParams = new HashMap<>();
    mailParams.put("from", "my@mail.com");
    mailParams.put("to", "their@mail.com");
    mailParams.put("template", new File(ClassLoader.getSystemResource("test.mustache").toURI()).toString());
    mailParams.put("attachments", Collections.singletonList(new File(ClassLoader.getSystemResource("attahcment.txt").toURI()).toString()));
    Map<String, Object> m = new HashMap<>();
    m.put("name", "Concord");
    m.put("workDir", "/");
    m.put("smtp", smtpParams);
    m.put("mail", mailParams);
    Context ctx = new MockContext(m);
    SmtpTask t = new SmtpTask();
    t.execute(ctx);
    MimeMessage[] messages = mail.getReceivedMessages();
    assertEquals(1, messages.length);
    MimeMessage msg = messages[0];
    assertNotNull(msg.getContent());
    assertTrue(msg.getContent() instanceof MimeMultipart);
    MimeMultipart mp = (MimeMultipart) msg.getContent();
    assertEquals(2, mp.getCount());
    assertEquals("Hello, Concord!", mp.getBodyPart(0).getContent());
    assertEquals("test-attachment", mp.getBodyPart(1).getContent());
    mail.reset();
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) Context(com.walmartlabs.concord.sdk.Context) MockContext(com.walmartlabs.concord.sdk.MockContext) SmtpServer(com.icegreen.greenmail.smtp.SmtpServer) HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) File(java.io.File) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 4 with SmtpServer

use of com.icegreen.greenmail.smtp.SmtpServer in project concord by walmartlabs.

the class SmtpTaskTest method testMultipleToComma.

@Test
public void testMultipleToComma() throws Exception {
    SmtpServer server = mail.getSmtp();
    Map<String, Object> smtpParams = new HashMap<>();
    smtpParams.put("host", "localhost");
    smtpParams.put("port", server.getPort());
    Map<String, Object> mailParams = new HashMap<>();
    mailParams.put("from", "my@mail.com");
    mailParams.put("to", "aaa@mail.com, bbb@mail.com");
    mailParams.put("subject", "test");
    mailParams.put("message", "Hello!");
    SmtpTask t = new SmtpTask();
    t.call(smtpParams, mailParams);
    MimeMessage[] messages = mail.getReceivedMessages();
    assertEquals(2, messages.length);
    assertEquals("Hello!\r\n", messages[0].getContent());
    mail.reset();
}
Also used : SmtpServer(com.icegreen.greenmail.smtp.SmtpServer) HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 5 with SmtpServer

use of com.icegreen.greenmail.smtp.SmtpServer in project concord by walmartlabs.

the class SmtpTaskTest method testNoBccViaCall.

@Test
public void testNoBccViaCall() throws Exception {
    SmtpServer server = mail.getSmtp();
    Map<String, Object> smtpParams = new HashMap<>();
    Map<String, Object> mailParams = new HashMap<>();
    smtpParams.put("host", "localhost");
    smtpParams.put("port", server.getPort());
    mailParams.put("from", "my@mail.com");
    mailParams.put("to", "their@mail.com");
    mailParams.put("subject", "test");
    mailParams.put("message", "Hello!");
    SmtpTask t = new SmtpTask();
    t.call(smtpParams, mailParams);
    MimeMessage[] messages = mail.getReceivedMessages();
    assertEquals(1, messages.length);
    assertEquals("Hello!\r\n", messages[0].getContent());
    assertEquals(1, messages[0].getAllRecipients().length);
    mail.reset();
}
Also used : SmtpServer(com.icegreen.greenmail.smtp.SmtpServer) HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Aggregations

SmtpServer (com.icegreen.greenmail.smtp.SmtpServer)12 MimeMessage (javax.mail.internet.MimeMessage)10 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)9 Test (org.junit.jupiter.api.Test)9 HashMap (java.util.HashMap)7 IOException (java.io.IOException)3 KeyStore (java.security.KeyStore)3 KeyStoreException (java.security.KeyStoreException)3 CertificateEncodingException (java.security.cert.CertificateEncodingException)3 Folder (javax.mail.Folder)3 MessagingException (javax.mail.MessagingException)3 Store (javax.mail.Store)3 Context (com.walmartlabs.concord.sdk.Context)2 MockContext (com.walmartlabs.concord.sdk.MockContext)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 GreenMailUser (com.icegreen.greenmail.user.GreenMailUser)1 EMailMessageModel (io.syndesis.connector.email.model.EMailMessageModel)1 Message (javax.mail.Message)1 MimeMultipart (javax.mail.internet.MimeMultipart)1