Search in sources :

Example 16 with Retriever

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

the class ImapServerTest method testRetrieveMultipart.

@Test
public void testRetrieveMultipart() throws Exception {
    assertThat(greenMail.getImap()).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.getImap())) {
        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 : Retriever(com.icegreen.greenmail.util.Retriever) MimeMessage(jakarta.mail.internet.MimeMessage) MimeMultipart(jakarta.mail.internet.MimeMultipart) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 17 with Retriever

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

the class ImapServerTest method testImapsReceive.

@Test
public void testImapsReceive() throws Throwable {
    assertThat(greenMail.getImaps()).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.getImaps())) {
        Message[] messages = retriever.getMessages(to);
        assertThat(messages.length).isEqualTo(1);
        assertThat(messages[0].getSubject()).isEqualTo(subject);
        assertThat(((String) messages[0].getContent()).trim()).isEqualTo(body);
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) MimeMessage(jakarta.mail.internet.MimeMessage) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 18 with Retriever

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

the class ImapServerTest method testRetrieveSimpleWithNonDefaultPassword.

@Test
public void testRetrieveSimpleWithNonDefaultPassword() throws Exception {
    assertThat(greenMail.getImap()).isNotNull();
    final String to = "test@localhost";
    final String password = "donotharmanddontrecipricateharm";
    greenMail.setUser(to, password);
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random();
    GreenMailUtil.sendTextEmailTest(to, "from@localhost", subject, body);
    greenMail.waitForIncomingEmail(5000, 1);
    try (Retriever retriever = new Retriever(greenMail.getImap())) {
        assertThatThrownBy(() -> retriever.getMessages(to, "wrongpassword")).isInstanceOf(RuntimeException.class).hasCauseInstanceOf(AuthenticationFailedException.class);
        Message[] messages = retriever.getMessages(to, password);
        assertThat(messages.length).isEqualTo(1);
        assertThat(messages[0].getSubject()).isEqualTo(subject);
        assertThat(((String) messages[0].getContent()).trim()).isEqualTo(body);
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) MimeMessage(jakarta.mail.internet.MimeMessage) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 19 with Retriever

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

the class ImapServerTest method testRetrieveSimple.

/**
 * Tests simple send and retrieve, including umlauts.
 *
 * @throws Exception on error.
 */
@Test
public void testRetrieveSimple() throws Exception {
    assertThat(greenMail.getImap()).isNotNull();
    final String subject = GreenMailUtil.random() + UMLAUTS;
    final String body = GreenMailUtil.random() + "\r\n" + " öäü \u00c4 \u00e4" + "\r\n" + GreenMailUtil.random();
    final String to = "test@localhost";
    MimeMessage mimeMessage = new MimeMessage(greenMail.getSmtp().createSession());
    mimeMessage.setSentDate(new Date());
    mimeMessage.setFrom("from@localhost");
    mimeMessage.setRecipients(Message.RecipientType.TO, to);
    // Need to explicitly set encoding
    mimeMessage.setSubject(subject, "UTF-8");
    mimeMessage.setText(body, "UTF-8");
    Transport.send(mimeMessage);
    greenMail.waitForIncomingEmail(5000, 1);
    try (Retriever retriever = new Retriever(greenMail.getImap())) {
        Message[] messages = retriever.getMessages(to);
        assertThat(messages.length).isEqualTo(1);
        assertThat(messages[0].getSubject()).isEqualTo(subject);
        assertThat(((String) messages[0].getContent()).trim()).isEqualTo(body);
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) MimeMessage(jakarta.mail.internet.MimeMessage) MimeMessage(jakarta.mail.internet.MimeMessage) Date(java.util.Date) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 20 with Retriever

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

the class SendReceiveWithDifferentEncodingsTest method testSendingAndRetrievingMaintainsEncoding.

private void testSendingAndRetrievingMaintainsEncoding(Charset charset) throws MessagingException, IOException {
    Session session = GreenMailUtil.getSession(ServerSetupTest.SMTP, new Properties());
    MimeMessage mimeMessage = new MimeMessage(session, mailDataInputStream(charset));
    GreenMailUtil.sendMimeMessage(mimeMessage);
    String sentMailText = new String(getBytes(mimeMessage), charset);
    assertThat(sentMailText).contains("Schön");
    greenMail.waitForIncomingEmail(1000, 1);
    Retriever retriever = new Retriever(greenMail.getPop3());
    MimeMessage receivedMessage = (MimeMessage) retriever.getMessages("bar@example.com")[0];
    // Verify that the Message's raw data is in the correct encoding
    String receivedPureMessage = new String(getBytes(receivedMessage), charset);
    assertThat(receivedPureMessage).contains("Schön");
    // Verify that the Message's 'getContent' method correctly determines the charset when returning the content.
    // Note that here, we retrieve a String without explicitly providing the encoding.
    String content = (String) receivedMessage.getContent();
    assertThat(content).contains("Schön");
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) MimeMessage(jakarta.mail.internet.MimeMessage) Properties(java.util.Properties) Session(jakarta.mail.Session)

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