Search in sources :

Example 1 with PlainSmimeDetails

use of org.simplejavamail.api.internal.smimesupport.model.PlainSmimeDetails 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)

Aggregations

Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 Email (org.simplejavamail.api.email.Email)1 PlainSmimeDetails (org.simplejavamail.api.internal.smimesupport.model.PlainSmimeDetails)1 EmailConverter.mimeMessageToEmail (org.simplejavamail.converter.EmailConverter.mimeMessageToEmail)1 InternalEmailPopulatingBuilder (org.simplejavamail.email.internal.InternalEmailPopulatingBuilder)1 MimeMessageAndEnvelope (testutil.testrules.MimeMessageAndEnvelope)1