Search in sources :

Example 1 with InternalEmailPopulatingBuilder

use of org.simplejavamail.email.internal.InternalEmailPopulatingBuilder 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 InternalEmailPopulatingBuilder

use of org.simplejavamail.email.internal.InternalEmailPopulatingBuilder in project simple-java-mail by bbottema.

the class EmailHelper method createDummyEmailBuilder.

public static EmailPopulatingBuilder createDummyEmailBuilder(@Nullable String id, boolean includeSubjectAndBody, boolean onlyBasicFields, boolean includeCustomHeaders, boolean useSmimeDetailsImplFromSmimeModule, final boolean fixSentDate, final boolean useDynamicImageEmbedding, final boolean includeCalendarText) throws IOException {
    EmailPopulatingBuilder builder = EmailBuilder.startingBlank().fixingMessageId(id).from("lollypop", "lol.pop@somemail.com").to("C.Cane", "candycane@candyshop.org");
    if (!onlyBasicFields) {
        // normally not needed, but for the test it is because the MimeMessage will
        // have it added automatically as well, so the parsed Email will also have it then
        builder = builder.withReplyTo("lollypop-reply", "lol.pop.reply@somemail.com").withBounceTo("lollypop-bounce", "lol.pop.bounce@somemail.com");
    }
    if (includeSubjectAndBody) {
        builder = builder.withSubject("hey").withPlainText("We should meet up!").withHTMLText("<b>We should meet up!</b><img src='cid:thumbsup'>");
    }
    if (useDynamicImageEmbedding) {
        builder = builder.appendTextHTML("<img src=\"/test-dynamicembedded-image/br2049.jpg\"/>");
    }
    if (includeCustomHeaders) {
        builder = builder.withHeader("dummyHeader", "dummyHeaderValue").withHeader("dummyHeader", "dummyHeaderValueSecond").withHeader("anotherDummyHeader", "anotherDummyHeaderValue").withDispositionNotificationTo("simple@address.com").withReturnReceiptTo("Complex Email", "simple@address.com");
    }
    if (includeCalendarText) {
        builder = builder.withCalendarText(CalendarMethod.CANCEL, "Sorry, can't make it");
    }
    if (fixSentDate) {
        builder = builder.fixingSentDate(CUSTOM_SENT_DATE);
    }
    // add two text files in different ways and a black thumbs up embedded image ->
    ByteArrayDataSource namedAttachment = new ByteArrayDataSource("Black Tie Optional", "text/plain");
    namedAttachment.setName("dresscode-ignored-because-of-override.txt");
    String base64String = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABeElEQVRYw2NgoAAYGxu3GxkZ7TY1NZVloDcAWq4MxH+B+D8Qv3FwcOCgtwM6oJaDMTAUXOhmuYqKCjvQ0pdoDrCnmwNMTEwakC0H4u8GBgYC9Ap6DSD+iewAoIPm0ctyLqBlp9F8/x+YE4zpYT8T0LL16JYD8U26+B7oyz4sloPwenpYno3DchCeROsUbwa05A8eB3wB4kqgIxOAuArIng7EW4H4EhC/B+JXQLwDaI4ryZaDSjeg5mt4LCcFXyIn1fdSyXJQVt1OtMWGhoai0OD8T0W8GohZifE1PxD/o7LlsPLiFNAKRrwOABWptLAcqc6QGDAHQEOAYaAc8BNotsJAOgAUAosG1AFA/AtUoY3YEFhKMAvS2AE7iC1+WaG1H6gY3gzE36hUFJ8mqzbU1dUVBBqQBzTgIDQRkWo5qCZdpaenJ0Zx1aytrc0DDB0foIG1oAYKqC0IZK8D4n1AfA6IzwPxXpCFoGoZVEUDaRGGUTAKRgEeAAA2eGJC+ETCiAAAAABJRU5ErkJggg==";
    InternalEmailPopulatingBuilder internalBuilder = ((InternalEmailPopulatingBuilder) builder.withAttachment("dresscode.txt", namedAttachment).withAttachment("location.txt", "On the moon!".getBytes(Charset.defaultCharset()), "text/plain").withEmbeddedImage("thumbsup", parseBase64Binary(base64String), "image/png")).withDecryptedAttachments(builder.getAttachments());
    if (useSmimeDetailsImplFromSmimeModule) {
        internalBuilder.withOriginalSmimeDetails(OriginalSmimeDetailsImpl.builder().build());
    }
    return internalBuilder;
}
Also used : EmailPopulatingBuilder(org.simplejavamail.api.email.EmailPopulatingBuilder) InternalEmailPopulatingBuilder(org.simplejavamail.email.internal.InternalEmailPopulatingBuilder) InternalEmailPopulatingBuilder(org.simplejavamail.email.internal.InternalEmailPopulatingBuilder) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource)

Aggregations

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