Search in sources :

Example 21 with Recipient

use of org.simplejavamail.api.email.Recipient in project simple-java-mail by bbottema.

the class MimeMessageHelper method setReplyTo.

/**
 * Fills the {@link Message} instance with reply-to address.
 *
 * @param email   The message in which the recipients are defined.
 * @param message The javax message that needs to be filled with reply-to address.
 * @throws UnsupportedEncodingException See {@link InternetAddress#InternetAddress(String, String)}.
 * @throws MessagingException           See {@link Message#setReplyTo(Address[])}
 */
static void setReplyTo(final Email email, final Message message) throws UnsupportedEncodingException, MessagingException {
    final Recipient replyToRecipient = email.getReplyToRecipient();
    if (replyToRecipient != null) {
        final InternetAddress replyToAddress = new InternetAddress(replyToRecipient.getAddress(), replyToRecipient.getName(), CHARACTER_ENCODING);
        message.setReplyTo(new Address[] { replyToAddress });
    }
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) Recipient(org.simplejavamail.api.email.Recipient)

Example 22 with Recipient

use of org.simplejavamail.api.email.Recipient in project simple-java-mail by bbottema.

the class MimeMessageHelper method setHeaders.

/**
 * Sets all headers on the {@link Message} instance. Since we're not using a high-level JavaMail method, the JavaMail library says we need to do
 * some encoding and 'folding' manually, to get the value right for the headers (see {@link MimeUtility}.
 * <p>
 * Furthermore sets the notification flags <code>Disposition-Notification-To</code> and <code>Return-Receipt-To</code> if provided. It used
 * JavaMail's built in method for producing an RFC compliant email address (see {@link InternetAddress#toString()}).
 *
 * @param email   The message in which the headers are defined.
 * @param message The {@link Message} on which to set the raw, encoded and folded headers.
 * @throws UnsupportedEncodingException See {@link MimeUtility#encodeText(String, String, String)}
 * @throws MessagingException           See {@link Message#addHeader(String, String)}
 * @see MimeUtility#encodeText(String, String, String)
 * @see MimeUtility#fold(int, String)
 */
static void setHeaders(final Email email, final Message message) throws UnsupportedEncodingException, MessagingException {
    // add headers (for raw message headers we need to 'fold' them using MimeUtility
    for (final Map.Entry<String, Collection<String>> header : email.getHeaders().entrySet()) {
        for (final String headerValue : header.getValue()) {
            final String headerName = header.getKey();
            final String headerValueEncoded = MimeUtility.encodeText(headerValue, CHARACTER_ENCODING, null);
            final String foldedHeaderValue = MimeUtility.fold(headerName.length() + 2, headerValueEncoded);
            message.addHeader(header.getKey(), foldedHeaderValue);
        }
    }
    if (email.isUseDispositionNotificationTo()) {
        final Recipient dispositionTo = checkNonEmptyArgument(email.getDispositionNotificationTo(), "dispositionNotificationTo");
        final Address address = new InternetAddress(dispositionTo.getAddress(), dispositionTo.getName(), CHARACTER_ENCODING);
        message.setHeader("Disposition-Notification-To", address.toString());
    }
    if (email.isUseReturnReceiptTo()) {
        final Recipient returnReceiptTo = checkNonEmptyArgument(email.getReturnReceiptTo(), "returnReceiptTo");
        final Address address = new InternetAddress(returnReceiptTo.getAddress(), returnReceiptTo.getName(), CHARACTER_ENCODING);
        message.setHeader("Return-Receipt-To", address.toString());
    }
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) InternetAddress(jakarta.mail.internet.InternetAddress) Address(jakarta.mail.Address) Collection(java.util.Collection) Recipient(org.simplejavamail.api.email.Recipient) Map(java.util.Map)

Example 23 with Recipient

use of org.simplejavamail.api.email.Recipient in project simple-java-mail by bbottema.

the class MimeMessageHelper method setRecipients.

/**
 * Fills the {@link Message} instance with recipients from the {@link Email}.
 *
 * @param email   The message in which the recipients are defined.
 * @param message The javax message that needs to be filled with recipients.
 * @throws UnsupportedEncodingException See {@link InternetAddress#InternetAddress(String, String)}.
 * @throws MessagingException           See {@link Message#addRecipient(Message.RecipientType, Address)}
 */
static void setRecipients(final Email email, final Message message) throws UnsupportedEncodingException, MessagingException {
    for (final Recipient recipient : email.getRecipients()) {
        final Address address = new InternetAddress(recipient.getAddress(), recipient.getName(), CHARACTER_ENCODING);
        message.addRecipient(recipient.getType(), address);
    }
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) InternetAddress(jakarta.mail.internet.InternetAddress) Address(jakarta.mail.Address) Recipient(org.simplejavamail.api.email.Recipient)

Example 24 with Recipient

use of org.simplejavamail.api.email.Recipient in project simple-java-mail by bbottema.

the class EmailConverterTest method testOutlookUnsentDraft.

@Test
public void testOutlookUnsentDraft() {
    final Recipient time2talk = new Recipient("time2talk@online-convert.com", "time2talk@online-convert.com", TO);
    @NotNull Email msg = EmailConverter.outlookMsgToEmail(new File(RESOURCE_TEST_MESSAGES + "/unsent draft.msg"));
    EmailAssert.assertThat(msg).hasFromRecipient(new Recipient(null, "donotreply@unknown-from-address.net", null));
    EmailAssert.assertThat(msg).hasSubject("MSG Test File");
    EmailAssert.assertThat(msg).hasOnlyRecipients(time2talk);
    EmailAssert.assertThat(msg).hasNoAttachments();
    assertThat(msg.getPlainText()).isNotEmpty();
    assertThat(normalizeNewlines(msg.getHTMLText())).isNotEmpty();
}
Also used : Email(org.simplejavamail.api.email.Email) Recipient(org.simplejavamail.api.email.Recipient) NotNull(org.jetbrains.annotations.NotNull) File(java.io.File) Test(org.junit.Test)

Example 25 with Recipient

use of org.simplejavamail.api.email.Recipient in project simple-java-mail by bbottema.

the class EmailConverterTest method testOutlookBasicConversions.

@Test
public void testOutlookBasicConversions() {
    final Recipient elias = new Recipient("Elias Laugher", "elias.laugher@gmail.com", null);
    final Recipient sven = new Recipient("Sven Sielenkemper", "sielenkemper@otris.de", TO);
    final Recipient niklas = new Recipient("niklas.lindson@gmail.com", "niklas.lindson@gmail.com", CC);
    @NotNull Email msg = EmailConverter.outlookMsgToEmail(new File(RESOURCE_TEST_MESSAGES + "/simple email with TO and CC.msg"));
    EmailAssert.assertThat(msg).hasFromRecipient(elias);
    EmailAssert.assertThat(msg).hasSubject("Test E-Mail");
    EmailAssert.assertThat(msg).hasOnlyRecipients(sven, niklas);
    EmailAssert.assertThat(msg).hasNoAttachments();
    assertThat(msg.getPlainText()).isNotEmpty();
    assertThat(normalizeNewlines(msg.getHTMLText())).isEqualTo("<div dir=\"auto\">Just a test to get an email with one cc recipient.</div>\n");
    assertThat(normalizeNewlines(msg.getPlainText())).isEqualTo("Just a test to get an email with one cc recipient.\n");
}
Also used : Email(org.simplejavamail.api.email.Email) Recipient(org.simplejavamail.api.email.Recipient) NotNull(org.jetbrains.annotations.NotNull) File(java.io.File) Test(org.junit.Test)

Aggregations

Recipient (org.simplejavamail.api.email.Recipient)56 Test (org.junit.Test)43 Email (org.simplejavamail.api.email.Email)31 File (java.io.File)10 AttachmentResource (org.simplejavamail.api.email.AttachmentResource)7 InternetAddress (jakarta.mail.internet.InternetAddress)4 NotNull (org.jetbrains.annotations.NotNull)4 EmailPopulatingBuilder (org.simplejavamail.api.email.EmailPopulatingBuilder)4 EmailConverter.mimeMessageToEmail (org.simplejavamail.converter.EmailConverter.mimeMessageToEmail)4 Address (jakarta.mail.Address)2 MimeMessage (jakarta.mail.internet.MimeMessage)2 InputStream (java.io.InputStream)2 Collection (java.util.Collection)2 Map (java.util.Map)2 HashedMap (org.apache.commons.collections4.map.HashedMap)2 Property (org.simplejavamail.config.ConfigLoader.Property)2 InternalEmailPopulatingBuilder (org.simplejavamail.email.internal.InternalEmailPopulatingBuilder)2 Test (org.testng.annotations.Test)2 EmailHelper (testutil.EmailHelper)2 MimeMessageAndEnvelope (testutil.testrules.MimeMessageAndEnvelope)2