|
20 | 20 |
|
21 | 21 | package org.restcomm.connect.identity; |
22 | 22 |
|
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; |
24 | 27 |
|
25 | 28 | /** |
26 | 29 | * @author ddh.huy@gmail.com (Huy Dang) |
27 | 30 | */ |
28 | 31 | 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); |
31 | 33 |
|
32 | 34 | /** |
33 | | - * Verify an email address has valid format or not, use the default regex |
| 35 | + * Verify an email address has valid format or not |
34 | 36 | * |
35 | 37 | * @param email: the email address need to be verified |
36 | 38 | * @return true if email format is valid, false if email format is invalid |
37 | 39 | */ |
38 | 40 | 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; |
51 | 51 | } |
52 | 52 | } |
0 commit comments