Search in sources :

Example 6 with AttachmentResource

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

the class MimeMessageHelperTest method determineResourceName4.

@Test
public void determineResourceName4() throws IOException {
    AttachmentResource resource4 = new AttachmentResource("the resource", getDataSource("blahblah.txt"));
    assertThat(MimeMessageHelper.determineResourceName(resource4, false)).isEqualTo("the resource");
    assertThat(MimeMessageHelper.determineResourceName(resource4, true)).isEqualTo("the resource.txt");
}
Also used : AttachmentResource(org.simplejavamail.email.AttachmentResource) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with AttachmentResource

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

the class MimeMessageHelperTest method determineResourceName5.

@Test
public void determineResourceName5() throws IOException {
    AttachmentResource resource5 = new AttachmentResource("the resource", getDataSource("blahblah"));
    assertThat(MimeMessageHelper.determineResourceName(resource5, false)).isEqualTo("the resource");
    assertThat(MimeMessageHelper.determineResourceName(resource5, true)).isEqualTo("the resource");
}
Also used : AttachmentResource(org.simplejavamail.email.AttachmentResource) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with AttachmentResource

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

the class MimeMessageHelperTest method determineResourceName7.

@Test
public void determineResourceName7() throws IOException {
    AttachmentResource resource7 = new AttachmentResource("the resource.txt", getDataSource("blahblah"));
    assertThat(MimeMessageHelper.determineResourceName(resource7, false)).isEqualTo("the resource.txt");
    assertThat(MimeMessageHelper.determineResourceName(resource7, true)).isEqualTo("the resource.txt");
}
Also used : AttachmentResource(org.simplejavamail.email.AttachmentResource) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with AttachmentResource

use of org.simplejavamail.email.AttachmentResource 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)

Aggregations

AttachmentResource (org.simplejavamail.email.AttachmentResource)9 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Recipient (org.simplejavamail.email.Recipient)2 Map (java.util.Map)1 EmailConverter.mimeMessageToEmail (org.simplejavamail.converter.EmailConverter.mimeMessageToEmail)1 Email (org.simplejavamail.email.Email)1