Skip to content

Commit

Permalink
skip email address validation by default, can be turned on
Browse files Browse the repository at this point in the history
  • Loading branch information
bbottema committed Apr 29, 2015
1 parent a74b24d commit a82787c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
Expand Up @@ -8,9 +8,17 @@
* @see #EmailAddressValidationCriteria(boolean, boolean)
*/
public class EmailAddressValidationCriteria {

private final boolean allowDomainLiterals;
private final boolean allowQuotedIdentifiers;

/**
* Criteria which is most RFC 2822 compliant and allows all compiant address forms, including the more exotic ones.
*
* @see #EmailAddressValidationCriteria(boolean, boolean)
*/
public static final EmailAddressValidationCriteria RFC_COMPLIANT = new EmailAddressValidationCriteria(true, true);

/**
* @param allowDomainLiterals <ul>
* <li>This flag states whether domain literals are allowed in the email address, e.g.:
Expand All @@ -25,8 +33,7 @@ public class EmailAddressValidationCriteria {
* change this constant to <tt>false</tt>.
* <p>
* Its default value is <tt>true</tt> to remain RFC 2822 compliant, but you should set it depending on what you need for your
* application.
* </li>
* application.</li>
* </ul>
* @param allowQuotedIdentifiers <ul>
* <li>This flag states whether quoted identifiers are allowed (using quotes and angle brackets around the raw address) are
Expand All @@ -39,8 +46,7 @@ public class EmailAddressValidationCriteria {
* <tt>false</tt>.
* <p>
* Its default value is <tt>true</tt> to remain RFC 2822 compliant, but you should set it depending on what you need for your
* application.
* </li>
* application.</li>
* </ul>
*/
public EmailAddressValidationCriteria(boolean allowDomainLiterals, boolean allowQuotedIdentifiers) {
Expand Down
Expand Up @@ -37,10 +37,10 @@ private EmailValidationUtil() {
*
* @param email A complete email address.
* @return Whether the e-mail address is compliant with RFC 2822.
* @see EmailAddressValidationCriteria#EmailAddressValidationCriteria(boolean, boolean)
* @see EmailAddressValidationCriteria#RFC_COMPLIANT
*/
public static boolean isValid(final String email) {
return isValid(email, new EmailAddressValidationCriteria(true, true));
return isValid(email, EmailAddressValidationCriteria.RFC_COMPLIANT);
}

/**
Expand All @@ -49,7 +49,7 @@ public static boolean isValid(final String email) {
* @param email A complete email address.
* @param emailAddressValidationCriteria A set of flags that restrict or relax RFC 2822 compliance.
* @return Whether the e-mail address is compliant with RFC 2822, configured using the passed in {@link EmailAddressValidationCriteria}.
* @see EmailAddressValidationCriteria#EmailAddressValidationCriteria(boolean, boolean)
* @see EmailAddressValidationCriteria#RFC_COMPLIANT
*/
public static boolean isValid(final String email, final EmailAddressValidationCriteria emailAddressValidationCriteria) {
return buildValidEmailPattern(emailAddressValidationCriteria).matcher(email).matches();
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/org/codemonkey/simplejavamail/Mailer.java
Expand Up @@ -69,7 +69,7 @@
public class Mailer {

private static final Logger logger = LoggerFactory.getLogger(Mailer.class);

/**
* Encoding used for setting body text, email address, headers, reply-to fields etc. ({@value #CHARACTER_ENCODING}).
*/
Expand Down Expand Up @@ -101,22 +101,22 @@ public class Mailer {
* Default constructor, stores the given mail session for later use. Assumes that *all* properties used to make a connection are
* configured (host, port, authentication and transport protocol settings).
* <p>
* Also defines a default email address validation criteria object, which remains true to RFC 2822, meaning allowing both domain
* literals and quoted identifiers (see {@link EmailAddressValidationCriteria#EmailAddressValidationCriteria(boolean, boolean)}).
* Also leaves email address validation criteria empty so that no validation is being performed. Validation errors will come from the
* smtp server instead.
*
* @param session A preconfigured mail {@link Session} object with which a {@link Message} can be produced.
*/
public Mailer(final Session session) {
this.session = session;
this.emailAddressValidationCriteria = new EmailAddressValidationCriteria(true, true);
this.emailAddressValidationCriteria = null;
}

/**
* Overloaded constructor which produces a new {@link Session} on the fly. Use this if you don't have a mail session configured in your
* web container, or Spring context etc.
* <p>
* Also defines a default email address validation criteria object, which remains true to RFC 2822, meaning allowing both domain
* literals and quoted identifiers (see {@link EmailAddressValidationCriteria#EmailAddressValidationCriteria(boolean, boolean)}).
* Also leaves email address validation criteria empty so that no validation is being performed. Validation errors will come from the
* smtp server instead.
*
* @param host The address URL of the SMTP server to be used.
* @param port The port of the SMTP server.
Expand All @@ -134,7 +134,7 @@ public Mailer(final String host, final Integer port, final String username, fina
}
this.transportStrategy = transportStrategy;
this.session = createMailSession(host, port, username, password);
this.emailAddressValidationCriteria = new EmailAddressValidationCriteria(true, true);
this.emailAddressValidationCriteria = null;
}

/**
Expand Down Expand Up @@ -287,7 +287,7 @@ public boolean validate(final Email email)
throw new MailException(MailException.MISSING_RECIPIENT);
} else if (email.getFromRecipient() == null) {
throw new MailException(MailException.MISSING_SENDER);
} else {
} else if (emailAddressValidationCriteria != null) {
if (!EmailValidationUtil.isValid(email.getFromRecipient().getAddress(), emailAddressValidationCriteria)) {
throw new MailException(String.format(MailException.INVALID_SENDER, email));
}
Expand Down Expand Up @@ -362,7 +362,8 @@ private void setReplyTo(final Email email, final Message message)
throws UnsupportedEncodingException, MessagingException {
final Recipient replyToRecipient = email.getReplyToRecipient();
if (replyToRecipient != null) {
InternetAddress replyToAddress = new InternetAddress(replyToRecipient.getAddress(), replyToRecipient.getName(), CHARACTER_ENCODING);
InternetAddress replyToAddress = new InternetAddress(replyToRecipient.getAddress(), replyToRecipient.getName(),
CHARACTER_ENCODING);
message.setReplyTo(new Address[] { replyToAddress });
}
}
Expand Down Expand Up @@ -516,7 +517,7 @@ private class MimeEmailMessageWrapper {

/**
* Overrides the default email address validation restrictions when validating and sending emails using the current <code>Mailer</code>
* instance.
* instance. By default no validation will be performed by simple-java-mail, until a criteria object has been set.
*
* @param emailAddressValidationCriteria Refer to
* {@link EmailAddressValidationCriteria#EmailAddressValidationCriteria(boolean, boolean)}.
Expand Down

0 comments on commit a82787c

Please sign in to comment.