Search in sources :

Example 1 with MimeMessageAndEnvelope

use of testutil.testrules.MimeMessageAndEnvelope in project simple-java-mail by bbottema.

the class MailerLiveTest method assertSendingEmail.

private Email assertSendingEmail(final EmailPopulatingBuilder originalEmailPopulatingBuilder, boolean compensateForDresscodeAttachmentNameOverrideErasure, boolean skipChecksDueToSmime, boolean async, final boolean sentDateWasFixed, final boolean dynamicImageEmbeddingWasUsed) throws MessagingException, ExecutionException, InterruptedException {
    Email originalEmail = originalEmailPopulatingBuilder.buildEmail();
    if (!async) {
        mailer.sendMail(originalEmail);
    } else {
        verifyNonnullOrEmpty(mailer.sendMail(originalEmail, async)).get();
    }
    MimeMessageAndEnvelope receivedMimeMessage = smtpServerRule.getOnlyMessage();
    assertThat(receivedMimeMessage.getMimeMessage().getMessageID()).isEqualTo(originalEmail.getId());
    if (originalEmail.getBounceToRecipient() != null) {
        assertThat(receivedMimeMessage.getEnvelopeSender()).isEqualTo(originalEmail.getBounceToRecipient().getAddress());
    } else {
        assertThat(receivedMimeMessage.getEnvelopeSender()).isEqualTo(originalEmail.getFromRecipient().getAddress());
    }
    Email receivedEmail = mimeMessageToEmail(receivedMimeMessage.getMimeMessage(), loadPkcs12KeyStore());
    if (!sentDateWasFixed) {
        GregorianCalendar receiveWindowStart = new GregorianCalendar();
        receiveWindowStart.add(Calendar.SECOND, -10);
        assertThat(receivedEmail.getSentDate()).isBetween(receiveWindowStart.getTime(), new Date());
    } else {
        assertThat(receivedEmail.getSentDate()).isEqualTo(originalEmailPopulatingBuilder.getSentDate());
    }
    // ID will always be generated when sending: if set to a specific value, just assume the generated one
    if (originalEmailPopulatingBuilder.getId() == null) {
        originalEmailPopulatingBuilder.fixingMessageId(receivedEmail.getId());
    }
    // sent-date will always be generated when sending: if not set to a specific value, just assume the generated one
    if (originalEmailPopulatingBuilder.getSentDate() == null) {
        originalEmailPopulatingBuilder.fixingSentDate(verifyNonnullOrEmpty(receivedEmail.getSentDate()));
    }
    // hack: it seems Wiser automatically defaults replyTo address to the From address if left empty
    if (originalEmailPopulatingBuilder.getReplyToRecipient() == null) {
        originalEmailPopulatingBuilder.withReplyTo(originalEmailPopulatingBuilder.getFromRecipient());
    }
    // received email will always have an id, so let's make sure we're able to compare to the original email object
    if (originalEmailPopulatingBuilder.getHeaders().get("Message-ID") == null) {
        originalEmailPopulatingBuilder.withHeader("Message-ID", originalEmail.getId());
    }
    // bounce recipient is not part of the Mimemessage, but the Envelope and is configured on the Session and is not received back on the MimeMessage
    if (originalEmailPopulatingBuilder.getBounceToRecipient() != null) {
        originalEmailPopulatingBuilder.clearBounceTo();
    }
    if (originalEmailPopulatingBuilder.getOriginalSmimeDetails() instanceof PlainSmimeDetails) {
        // because the S/MIME module is loaded, the default PLAIN version gets replaced with the one from the module
        ((InternalEmailPopulatingBuilder) originalEmailPopulatingBuilder).withOriginalSmimeDetails(OriginalSmimeDetailsImpl.builder().build());
    }
    if (compensateForDresscodeAttachmentNameOverrideErasure) {
        TestDataHelper.fixDresscodeAttachment(receivedEmail);
    }
    if (!skipChecksDueToSmime) {
        // reading a signed mail is different from building a new one
        assertThat(receivedEmail).isEqualTo(originalEmailPopulatingBuilder.buildEmail());
    }
    return receivedEmail;
}
Also used : Email(org.simplejavamail.api.email.Email) EmailConverter.mimeMessageToEmail(org.simplejavamail.converter.EmailConverter.mimeMessageToEmail) MimeMessageAndEnvelope(testutil.testrules.MimeMessageAndEnvelope) GregorianCalendar(java.util.GregorianCalendar) InternalEmailPopulatingBuilder(org.simplejavamail.email.internal.InternalEmailPopulatingBuilder) Date(java.util.Date) PlainSmimeDetails(org.simplejavamail.api.internal.smimesupport.model.PlainSmimeDetails)

Example 2 with MimeMessageAndEnvelope

use of testutil.testrules.MimeMessageAndEnvelope in project simple-java-mail by bbottema.

the class MailerLiveTest method createMailSession_ReplyToMessage_NotAll_AndCustomReferences.

@Test
public void createMailSession_ReplyToMessage_NotAll_AndCustomReferences() throws MessagingException, ExecutionException, InterruptedException {
    // send initial mail
    mailer.sendMail(readOutlookMessage("test-messages/HTML mail with replyto and attachment and embedded image.msg").buildEmail());
    MimeMessageAndEnvelope receivedMimeMessage = smtpServerRule.getOnlyMessage();
    EmailPopulatingBuilder receivedEmailPopulatingBuilder = mimeMessageToEmailBuilder(receivedMimeMessage.getMimeMessage());
    // send reply to initial mail
    Email reply = EmailBuilder.replyingTo(assertSendingEmail(receivedEmailPopulatingBuilder, false, false, false, true, false)).from("Moo Shmoo", "dummy@domain.com").withPlainText("This is the reply").buildEmail();
    // test received reply to initial mail
    mailer.sendMail(reply);
    MimeMessage receivedMimeMessageReply = smtpServerRule.getOnlyMessage("lo.pop.replyto@somemail.com");
    Email receivedReply = mimeMessageToEmail(receivedMimeMessageReply);
    EmailAssert.assertThat(receivedReply).hasSubject("Re: hey");
    EmailAssert.assertThat(receivedReply).hasOnlyRecipients(new Recipient("lollypop-replyto", "lo.pop.replyto@somemail.com", TO));
    assertThat(receivedReply.getHeaders()).contains(entry("In-Reply-To", singletonList(receivedEmailPopulatingBuilder.getId())));
    assertThat(receivedReply.getHeaders()).contains(entry("References", singletonList(receivedEmailPopulatingBuilder.getId())));
    EmailPopulatingBuilder receivedEmailReplyPopulatingBuilder = mimeMessageToEmailBuilder(receivedMimeMessageReply);
    Email replyToReply = EmailBuilder.replyingTo(assertSendingEmail(receivedEmailReplyPopulatingBuilder, false, false, false, false, false)).from("Pappa Moo", "dummy@domain.com").withPlainText("This is the reply to the reply").buildEmail();
    // test received reply to initial mail
    mailer.sendMail(replyToReply);
    MimeMessage receivedMimeMessageReplyToReply = smtpServerRule.getOnlyMessage("dummy@domain.com");
    Email receivedReplyToReply = mimeMessageToEmail(receivedMimeMessageReplyToReply);
    EmailAssert.assertThat(receivedReplyToReply).hasSubject("Re: hey");
    EmailAssert.assertThat(receivedReplyToReply).hasOnlyRecipients(new Recipient("Moo Shmoo", "dummy@domain.com", TO));
    assertThat(receivedReplyToReply.getHeaders()).contains(entry("In-Reply-To", singletonList(receivedEmailReplyPopulatingBuilder.getId())));
    assertThat(receivedReplyToReply.getHeaders()).contains(entry("References", singletonList(MimeUtility.fold("References: ".length(), format("%s\n%s", receivedEmailPopulatingBuilder.getId(), receivedEmailReplyPopulatingBuilder.getId())))));
}
Also used : Email(org.simplejavamail.api.email.Email) EmailConverter.mimeMessageToEmail(org.simplejavamail.converter.EmailConverter.mimeMessageToEmail) MimeMessage(jakarta.mail.internet.MimeMessage) MimeMessageAndEnvelope(testutil.testrules.MimeMessageAndEnvelope) EmailPopulatingBuilder(org.simplejavamail.api.email.EmailPopulatingBuilder) InternalEmailPopulatingBuilder(org.simplejavamail.email.internal.InternalEmailPopulatingBuilder) Recipient(org.simplejavamail.api.email.Recipient) Test(org.junit.Test)

Example 3 with MimeMessageAndEnvelope

use of testutil.testrules.MimeMessageAndEnvelope in project simple-java-mail by bbottema.

the class MailerLiveTest method createMailSession_ReplyToMessage.

@Test
public void createMailSession_ReplyToMessage() throws MessagingException, ExecutionException, InterruptedException {
    // send initial mail
    mailer.sendMail(readOutlookMessage("test-messages/HTML mail with replyto and attachment and embedded image.msg").buildEmail());
    MimeMessageAndEnvelope receivedMimeMessage = smtpServerRule.getOnlyMessage();
    EmailPopulatingBuilder receivedEmailPopulatingBuilder = mimeMessageToEmailBuilder(receivedMimeMessage.getMimeMessage());
    // send reply to initial mail
    Email reply = EmailBuilder.replyingToAll(assertSendingEmail(receivedEmailPopulatingBuilder, false, false, false, true, false)).from("dummy@domain.com").withPlainText("This is the reply").buildEmail();
    // test received reply to initial mail
    mailer.sendMail(reply);
    MimeMessage receivedMimeMessageReply1 = smtpServerRule.getMessage("lo.pop.replyto@somemail.com");
    MimeMessage receivedMimeMessageReply2 = smtpServerRule.getMessage("benny.bottema@aegon.nl");
    Email receivedReply1 = mimeMessageToEmail(receivedMimeMessageReply1);
    Email receivedReply2 = mimeMessageToEmail(receivedMimeMessageReply2);
    assertThat(receivedReply1).isEqualTo(receivedReply2);
    EmailAssert.assertThat(receivedReply1).hasSubject("Re: hey");
    EmailAssert.assertThat(receivedReply1).hasOnlyRecipients(new Recipient("lollypop-replyto", "lo.pop.replyto@somemail.com", TO), new Recipient("Bottema, Benny", "benny.bottema@aegon.nl", TO));
    assertThat(receivedReply1.getHeaders()).contains(entry("In-Reply-To", singletonList(receivedEmailPopulatingBuilder.getId())));
    assertThat(receivedReply1.getHeaders()).contains(entry("References", singletonList(receivedEmailPopulatingBuilder.getId())));
}
Also used : Email(org.simplejavamail.api.email.Email) EmailConverter.mimeMessageToEmail(org.simplejavamail.converter.EmailConverter.mimeMessageToEmail) MimeMessage(jakarta.mail.internet.MimeMessage) MimeMessageAndEnvelope(testutil.testrules.MimeMessageAndEnvelope) EmailPopulatingBuilder(org.simplejavamail.api.email.EmailPopulatingBuilder) InternalEmailPopulatingBuilder(org.simplejavamail.email.internal.InternalEmailPopulatingBuilder) Recipient(org.simplejavamail.api.email.Recipient) Test(org.junit.Test)

Example 4 with MimeMessageAndEnvelope

use of testutil.testrules.MimeMessageAndEnvelope in project simple-java-mail by bbottema.

the class MailerSOCKSLiveTest method assertSendingEmail.

private void assertSendingEmail(final Mailer mailer, final EmailPopulatingBuilder originalEmailPopulatingBuilder, boolean async) throws Exception {
    Email originalEmail = originalEmailPopulatingBuilder.buildEmail();
    if (!async) {
        mailer.sendMail(originalEmail);
    } else {
        verifyNonnullOrEmpty(mailer.sendMail(originalEmail, async)).get();
    }
    MimeMessageAndEnvelope receivedMimeMessage = smtpServerRule.getOnlyMessage();
    assertThat(receivedMimeMessage.getMimeMessage().getMessageID()).isEqualTo(originalEmail.getId());
}
Also used : Email(org.simplejavamail.api.email.Email) MimeMessageAndEnvelope(testutil.testrules.MimeMessageAndEnvelope)

Aggregations

Email (org.simplejavamail.api.email.Email)4 MimeMessageAndEnvelope (testutil.testrules.MimeMessageAndEnvelope)4 EmailConverter.mimeMessageToEmail (org.simplejavamail.converter.EmailConverter.mimeMessageToEmail)3 InternalEmailPopulatingBuilder (org.simplejavamail.email.internal.InternalEmailPopulatingBuilder)3 MimeMessage (jakarta.mail.internet.MimeMessage)2 Test (org.junit.Test)2 EmailPopulatingBuilder (org.simplejavamail.api.email.EmailPopulatingBuilder)2 Recipient (org.simplejavamail.api.email.Recipient)2 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 PlainSmimeDetails (org.simplejavamail.api.internal.smimesupport.model.PlainSmimeDetails)1