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;
}
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())))));
}
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())));
}
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());
}
Aggregations