Skip to content

Commit da50dcf

Browse files
committed
validate email format using JavaMail API instead of regex
1 parent 398ccf0 commit da50dcf

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

restcomm/restcomm.identity/src/main/java/org/restcomm/connect/identity/EmailValidator.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,33 @@
2020

2121
package org.restcomm.connect.identity;
2222

23-
import java.util.regex.Pattern;
23+
import javax.mail.internet.AddressException;
24+
import javax.mail.internet.InternetAddress;
25+
26+
import org.apache.log4j.Logger;
2427

2528
/**
2629
* @author ddh.huy@gmail.com (Huy Dang)
2730
*/
2831
public class EmailValidator {
29-
private static final String VALID_EMAIL_ADDRESS_REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
30-
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
32+
private static Logger logger = Logger.getLogger(EmailValidator.class);
3133

3234
/**
33-
* Verify an email address has valid format or not, use the default regex
35+
* Verify an email address has valid format or not
3436
*
3537
* @param email: the email address need to be verified
3638
* @return true if email format is valid, false if email format is invalid
3739
*/
3840
public static boolean isValidEmailFormat ( String email ) {
39-
return isValidEmailFormat(email, VALID_EMAIL_ADDRESS_REGEX);
40-
}
41-
42-
/**
43-
* Verify an email address has valid format or not, use the customized user input regex
44-
*
45-
* @param email: the email address need to be verified
46-
* @return true if email format is valid, false if email format is invalid
47-
*/
48-
public static boolean isValidEmailFormat ( String email, String regex ) {
49-
Pattern pattern = Pattern.compile(regex);
50-
return pattern.matcher(email).matches();
41+
boolean isValid = false;
42+
try {
43+
InternetAddress emailAddress = new InternetAddress(email);
44+
emailAddress.validate();
45+
isValid = true;
46+
} catch (AddressException ex) {
47+
isValid = false;
48+
logger.error("Email " + email + " is invalid");
49+
}
50+
return isValid;
5151
}
5252
}

0 commit comments

Comments
 (0)