Search in sources :

Example 6 with Retriever

use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.

the class GreenMailUtilTest method testGetEmptyBodyAndHeader.

@Test
public void testGetEmptyBodyAndHeader() throws Exception {
    GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
    try {
        greenMail.start();
        String subject = GreenMailUtil.random();
        // Provokes https://github.com/greenmail-mail-test/greenmail/issues/151
        String body = "";
        String to = "test@localhost";
        final byte[] gifAttachment = { 0, 1, 2 };
        GreenMailUtil.sendAttachmentEmail(to, "from@localhost", subject, body, gifAttachment, "image/gif", "testimage_filename", "testimage_description", greenMail.getSmtp().getServerSetup());
        greenMail.waitForIncomingEmail(5000, 1);
        try (Retriever retriever = new Retriever(greenMail.getImap())) {
            MimeMultipart mp = (MimeMultipart) retriever.getMessages(to)[0].getContent();
            BodyPart bp;
            bp = mp.getBodyPart(0);
            assertThat(body).isEqualTo(GreenMailUtil.getBody(bp).trim());
            assertThat("Content-Type: text/plain; charset=us-ascii\r\n" + "Content-Transfer-Encoding: 7bit").isEqualTo(GreenMailUtil.getHeaders(bp).trim());
            bp = mp.getBodyPart(1);
            assertThat("AAEC").isEqualTo(GreenMailUtil.getBody(bp).trim());
            assertThat("Content-Type: image/gif; name=testimage_filename\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-Disposition: attachment; filename=testimage_filename\r\n" + "Content-Description: testimage_description").isEqualTo(GreenMailUtil.getHeaders(bp).trim());
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            GreenMailUtil.copyStream(bp.getInputStream(), bout);
            assertThat(bout.toByteArray()).isEqualTo(gifAttachment);
        }
    } finally {
        greenMail.stop();
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) MimeMultipart(jakarta.mail.internet.MimeMultipart) GreenMail(com.icegreen.greenmail.util.GreenMail) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest) Test(org.junit.Test)

Example 7 with Retriever

use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.

the class Pop3ServerTest method testPop3sReceive.

@Test
public void testPop3sReceive() throws Throwable {
    assertThat(greenMail.getPop3s()).isNotNull();
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random();
    String to = "test@localhost";
    GreenMailUtil.sendTextEmailSecureTest(to, "from@localhost", subject, body);
    greenMail.waitForIncomingEmail(5000, 1);
    try (Retriever retriever = new Retriever(greenMail.getPop3s())) {
        Message[] messages = retriever.getMessages(to);
        assertThat(messages.length).isEqualTo(1);
        assertThat(messages[0].getSubject()).isEqualTo(subject);
        assertThat(GreenMailUtil.getBody(messages[0]).trim()).isEqualTo(body);
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) Message(jakarta.mail.Message) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 8 with Retriever

use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.

the class Pop3ServerTest method testRetrieveMultipart.

@Test
public void testRetrieveMultipart() throws Exception {
    assertThat(greenMail.getPop3()).isNotNull();
    String subject = GreenMailUtil.random();
    String body = GreenMailUtil.random();
    String to = "test@localhost";
    GreenMailUtil.sendAttachmentEmail(to, "from@localhost", subject, body, new byte[] { 0, 1, 2 }, "image/gif", "testimage_filename", "testimage_description", ServerSetupTest.SMTP);
    greenMail.waitForIncomingEmail(5000, 1);
    try (Retriever retriever = new Retriever(greenMail.getPop3())) {
        Message[] messages = retriever.getMessages(to);
        Object o = messages[0].getContent();
        assertThat(o instanceof MimeMultipart).isTrue();
        MimeMultipart mp = (MimeMultipart) o;
        assertThat(mp.getCount()).isEqualTo(2);
        BodyPart bp;
        bp = mp.getBodyPart(0);
        assertThat(GreenMailUtil.getBody(bp).trim()).isEqualTo(body);
        bp = mp.getBodyPart(1);
        assertThat(GreenMailUtil.getBody(bp).trim()).isEqualTo("AAEC");
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        GreenMailUtil.copyStream(bp.getInputStream(), bout);
        byte[] gif = bout.toByteArray();
        for (int i = 0; i < gif.length; i++) {
            assertThat((int) gif[i]).isEqualTo(i);
        }
    }
}
Also used : BodyPart(jakarta.mail.BodyPart) Retriever(com.icegreen.greenmail.util.Retriever) Message(jakarta.mail.Message) MimeMultipart(jakarta.mail.internet.MimeMultipart) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 9 with Retriever

use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.

the class SenderRecipientTest method retrieveAndCheck.

/**
 * Retrieve message from retriever and check the sender and receivers
 *
 * @param server Server to read from
 * @param login  Account to retrieve
 */
private void retrieveAndCheck(AbstractServer server, String login) throws MessagingException {
    try (Retriever retriever = new Retriever(server)) {
        Message[] messages = retriever.getMessages(login);
        assertThat(messages.length).isEqualTo(1);
        Message message = messages[0];
        assertThat(toInetAddr(message.getRecipients(Message.RecipientType.TO))).isEqualTo(TO_ADDRESSES);
        assertThat(toInetAddr(message.getRecipients(Message.RecipientType.CC))).isEqualTo(CC_ADDRESSES);
        // BCC addresses are not contained in the message since other receivers are not allowed to know the list of
        // BCC recipients
        assertThat(toInetAddr(message.getRecipients(Message.RecipientType.BCC))).isNull();
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) Message(jakarta.mail.Message) MimeMessage(jakarta.mail.internet.MimeMessage)

Example 10 with Retriever

use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.

the class GreenMailConfigurationTestBase method testUsersAccessible.

/**
 * Check if both user accounts can be accessed
 */
public static void testUsersAccessible(GreenMailOperations greenMail) {
    // Checks if the user that is registered in the config is actually accessible
    try (Retriever retriever = new Retriever(greenMail.getImap())) {
        final Message[] messages = retriever.getMessages("user@localhost", "password");
        // if getMessage is successful this means that the user account has been created
        assertEquals(messages.length, 0);
        // Now check second user. this one has a different user id
        final Message[] messages2 = retriever.getMessages("secondUserLogin", "password2");
        assertEquals(messages2.length, 0);
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) Message(jakarta.mail.Message)

Aggregations

Retriever (com.icegreen.greenmail.util.Retriever)25 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)17 Message (jakarta.mail.Message)17 Test (org.junit.Test)15 MimeMessage (jakarta.mail.internet.MimeMessage)11 MimeMultipart (jakarta.mail.internet.MimeMultipart)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IMAPMessage (com.sun.mail.imap.IMAPMessage)2 BodyPart (jakarta.mail.BodyPart)2 InputStream (java.io.InputStream)2 URI (java.net.URI)2 Date (java.util.Date)2 Message (javax.mail.Message)2 Test (org.junit.jupiter.api.Test)2 EditUserDTO (com.github.nkonev.aaa.dto.EditUserDTO)1 GreenMailMimeMessage (com.icegreen.greenmail.test.util.GreenMailMimeMessage)1 GreenMail (com.icegreen.greenmail.util.GreenMail)1 POP3Folder (com.sun.mail.pop3.POP3Folder)1 Session (jakarta.mail.Session)1 Properties (java.util.Properties)1