-2

This is my working screen shot:

When I Am trying to put .(dot) in front of the email address it's still showing Email send successfully:

Am putting my validation code:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Validation {   

    public static boolean isValidEmail(String email)
    {
    String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
    Pattern p = java.util.regex.Pattern.compile(ePattern);
    Matcher m = p.matcher(email);
    return m.matches();
    }
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138

2 Answers2

1

If I understand you correctly, your immediate problem is that an email starting with a dot is validated; you need to formulate the condition in a way that allows a dot only after the first character. For example:

String ePattern = "^[\\w-_]+[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";

A better way might be to use Apache Commons EmailValidator:

EmailValidator ev = EmailValidator.getInstance();
return ev.isValid(email);

Here's the maven dependency:

<dependency>
    <groupId>commons-validator</groupId>
    <artifactId>commons-validator</artifactId>
    <version>1.4.0</version>
</dependency>
JensS
  • 1,151
  • 2
  • 13
  • 20
0

You can use JAVA EE 7 API Library for e-mail check. With the help of the code block above, your program can validate the e-mail address inputs.

public boolean isValidEmailAddress(String email) 
{
    boolean result = true;
    try 
    {
       InternetAddress emailAddr = new InternetAddress(email);
       emailAddr.validate();
    } 
    catch (AddressException ex) 
    {
       result = false;
    }
    return result;
}  

Do not forget to import these as well:

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

After validatiton, you can send the desired mail. I am not sure but I hope it will help.

Have a nice day!

Ahmet Batu Orhan
  • 118
  • 2
  • 14
  • Which API, can you tell me the exact name of that API.so that i can download and use. – Rahul Bhosale Aug 21 '17 at 11:36
  • First, be sure that `JAVA EE Base` and `EJB and EAR` were added (I do not remember which one is related, just download both of them in case of everything. You can do it from `Tools -> Plugins -> Available Plugins`. It is located the upper part of your IDE (For NetBeans). If you are using something different, search how to add Plugins. You can also check them from `Installed` list inside `Plugins`). Then right-click the to libraries folder inside your project, find `JAVA EE 7 API Library` and add this library to your project. I hope I was clear and hope it will work. – Ahmet Batu Orhan Aug 21 '17 at 11:50