Search in sources :

Example 6 with Recipient

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

the class MailerLiveTest method createMailSession_ReplyToMessage_NotAll_AndCustomReferences.

@Test
public void createMailSession_ReplyToMessage_NotAll_AndCustomReferences() throws IOException, MessagingException {
    // send initial mail
    mailer.sendMail(readOutlookMessage("test-messages/HTML mail with replyto and attachment and embedded image.msg").buildEmail());
    MimeMessage receivedMimeMessage = smtpServerRule.getOnlyMessage();
    EmailPopulatingBuilder receivedEmailPopulatingBuilder = mimeMessageToEmailBuilder(receivedMimeMessage);
    // send reply to initial mail
    Email reply = EmailBuilder.replyingTo(assertSendingEmail(receivedEmailPopulatingBuilder)).withHeader("References", "dummy-references").from("dummy@domain.com").withPlainText("This is the reply").buildEmail();
    // test received reply to initial mail
    mailer.sendMail(reply);
    MimeMessage receivedMimeMessageReply1 = smtpServerRule.getOnlyMessage("lo.pop.replyto@somemail.com");
    Email receivedReply = mimeMessageToEmail(receivedMimeMessageReply1);
    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", receivedEmailPopulatingBuilder.getId()));
    assertThat(receivedReply.getHeaders()).contains(entry("References", "dummy-references"));
}
Also used : EmailConverter.mimeMessageToEmail(org.simplejavamail.converter.EmailConverter.mimeMessageToEmail) Email(org.simplejavamail.email.Email) MimeMessage(javax.mail.internet.MimeMessage) EmailPopulatingBuilder(org.simplejavamail.email.EmailPopulatingBuilder) Recipient(org.simplejavamail.email.Recipient) Test(org.junit.Test)

Example 7 with Recipient

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

the class MiscUtil method interpretRecipient.

/**
 * @param name         The name to use as fixed name or as default (depending on <code>fixedName</code> flag). Regardless of that flag, if a name
 *                     is <code>null</code>, the other one will be used.
 * @param fixedName    Determines if the given name should be used as override.
 * @param emailAddress An RFC2822 compliant email address, which can contain a name inside as well.
 */
@Nonnull
public static Recipient interpretRecipient(@Nullable final String name, boolean fixedName, @Nonnull final String emailAddress, @Nullable final RecipientType type) {
    try {
        final InternetAddress parsedAddress = InternetAddress.parse(emailAddress, false)[0];
        final String relevantName = (fixedName || parsedAddress.getPersonal() == null) ? defaultTo(name, parsedAddress.getPersonal()) : defaultTo(parsedAddress.getPersonal(), name);
        return new Recipient(relevantName, parsedAddress.getAddress(), type);
    } catch (final AddressException e) {
        // library take care of it when sending the email
        return new Recipient(name, emailAddress, type);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException) Recipient(org.simplejavamail.email.Recipient) Integer.toHexString(java.lang.Integer.toHexString) Nonnull(javax.annotation.Nonnull)

Example 8 with Recipient

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

the class Mailer method validate.

/**
 * Validates an {@link Email} instance. Validation fails if the subject is missing, content is missing, or no recipients are defined or that
 * the addresses are missing for NPM notification flags.
 * <p>
 * It also checks for illegal characters that would facilitate injection attacks:
 * <p>
 * <ul>
 * <li>http://www.cakesolutions.net/teamblogs/2008/05/08/email-header-injection-security</li>
 * <li>https://security.stackexchange.com/a/54100/110048</li>
 * <li>https://www.owasp.org/index.php/Testing_for_IMAP/SMTP_Injection_(OTG-INPVAL-011)</li>
 * <li>http://cwe.mitre.org/data/definitions/93.html</li>
 * </ul>
 *
 * @param email The email that needs to be configured correctly.
 *
 * @return Always <code>true</code> (throws a {@link MailException} exception if validation fails).
 * @throws MailException Is being thrown in any of the above causes.
 * @see EmailAddressValidator
 */
@SuppressWarnings({ "SameReturnValue", "WeakerAccess" })
public boolean validate(final Email email) throws MailException {
    // check for mandatory values
    if (email.getRecipients().size() == 0) {
        throw new MailerException(MailerException.MISSING_RECIPIENT);
    } else if (email.getFromRecipient() == null) {
        throw new MailerException(MailerException.MISSING_SENDER);
    } else if (email.isUseDispositionNotificationTo() && email.getDispositionNotificationTo() == null) {
        throw new MailerException(MailerException.MISSING_DISPOSITIONNOTIFICATIONTO);
    } else if (email.isUseReturnReceiptTo() && email.getReturnReceiptTo() == null) {
        throw new MailerException(MailerException.MISSING_RETURNRECEIPTTO);
    } else if (emailAddressCriteria != null) {
        if (!EmailAddressValidator.isValid(email.getFromRecipient().getAddress(), emailAddressCriteria)) {
            throw new MailerException(format(MailerException.INVALID_SENDER, email));
        }
        for (final Recipient recipient : email.getRecipients()) {
            if (!EmailAddressValidator.isValid(recipient.getAddress(), emailAddressCriteria)) {
                throw new MailerException(format(MailerException.INVALID_RECIPIENT, email));
            }
        }
        if (email.getReplyToRecipient() != null && !EmailAddressValidator.isValid(email.getReplyToRecipient().getAddress(), emailAddressCriteria)) {
            throw new MailerException(format(MailerException.INVALID_REPLYTO, email));
        }
        if (email.getBounceToRecipient() != null && !EmailAddressValidator.isValid(email.getBounceToRecipient().getAddress(), emailAddressCriteria)) {
            throw new MailerException(format(MailerException.INVALID_BOUNCETO, email));
        }
        if (email.isUseDispositionNotificationTo() && !EmailAddressValidator.isValid(email.getDispositionNotificationTo().getAddress(), emailAddressCriteria)) {
            throw new MailerException(format(MailerException.INVALID_DISPOSITIONNOTIFICATIONTO, email));
        }
        if (email.isUseReturnReceiptTo() && !EmailAddressValidator.isValid(email.getReturnReceiptTo().getAddress(), emailAddressCriteria)) {
            throw new MailerException(format(MailerException.INVALID_RETURNRECEIPTTO, email));
        }
    }
    // check for illegal values
    scanForInjectionAttack(email.getSubject(), "email.subject");
    for (final Map.Entry<String, String> headerEntry : email.getHeaders().entrySet()) {
        scanForInjectionAttack(headerEntry.getKey(), "email.header.mapEntryKey");
        scanForInjectionAttack(headerEntry.getValue(), "email.header." + headerEntry.getKey());
    }
    for (final AttachmentResource attachment : email.getAttachments()) {
        scanForInjectionAttack(attachment.getName(), "email.attachment.name");
    }
    for (final AttachmentResource embeddedImage : email.getEmbeddedImages()) {
        scanForInjectionAttack(embeddedImage.getName(), "email.embeddedImage.name");
    }
    scanForInjectionAttack(email.getFromRecipient().getName(), "email.fromRecipient.name");
    scanForInjectionAttack(email.getFromRecipient().getAddress(), "email.fromRecipient.address");
    if (!valueNullOrEmpty(email.getReplyToRecipient())) {
        scanForInjectionAttack(email.getReplyToRecipient().getName(), "email.replyToRecipient.name");
        scanForInjectionAttack(email.getReplyToRecipient().getAddress(), "email.replyToRecipient.address");
    }
    if (!valueNullOrEmpty(email.getBounceToRecipient())) {
        scanForInjectionAttack(email.getBounceToRecipient().getName(), "email.bounceToRecipient.name");
        scanForInjectionAttack(email.getBounceToRecipient().getAddress(), "email.bounceToRecipient.address");
    }
    for (final Recipient recipient : email.getRecipients()) {
        scanForInjectionAttack(recipient.getName(), "email.recipient.name");
        scanForInjectionAttack(recipient.getAddress(), "email.recipient.address");
    }
    return true;
}
Also used : AttachmentResource(org.simplejavamail.email.AttachmentResource) Recipient(org.simplejavamail.email.Recipient) Map(java.util.Map)

Example 9 with Recipient

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

the class MailSender method configureBounceToAddress.

private void configureBounceToAddress(final Session session, final Email email) {
    final Recipient bounceAddress = email.getBounceToRecipient();
    if (bounceAddress != null) {
        if (transportStrategy != null) {
            final String formattedRecipient = format("%s <%s>", bounceAddress.getName(), bounceAddress.getAddress());
            session.getProperties().setProperty(transportStrategy.propertyNameEnvelopeFrom(), formattedRecipient);
        } else {
            throw new MailSenderException(MailSenderException.CANNOT_SET_BOUNCETO_WITHOUT_TRANSPORTSTRATEGY);
        }
    }
}
Also used : Recipient(org.simplejavamail.email.Recipient)

Aggregations

Recipient (org.simplejavamail.email.Recipient)9 Test (org.junit.Test)4 InternetAddress (javax.mail.internet.InternetAddress)3 EmailConverter.mimeMessageToEmail (org.simplejavamail.converter.EmailConverter.mimeMessageToEmail)3 Email (org.simplejavamail.email.Email)3 MimeMessage (javax.mail.internet.MimeMessage)2 AttachmentResource (org.simplejavamail.email.AttachmentResource)2 EmailPopulatingBuilder (org.simplejavamail.email.EmailPopulatingBuilder)2 Integer.toHexString (java.lang.Integer.toHexString)1 Map (java.util.Map)1 Nonnull (javax.annotation.Nonnull)1 Address (javax.mail.Address)1 AddressException (javax.mail.internet.AddressException)1