Saturday, March 24, 2012

How To Validate Email Address in Java Mail API

1 comments

Validate Email Address using Java Mail API


In this tutorial you will find how to validate email address using java Mail API. You must to import javax.mail.internet.InternetAddress 



package jsupport;

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

/**
 *
 * @author Jsupport http://javasrilankansupport.blogspot.com
 */
public class ValidateEmailAddress {

    public static  boolean  validateEmail(String email) {
        try {
            new InternetAddress(email).validate();

        } catch (AddressException ex) {
            System.out.println("Error : " + ex.getMessage());
            return false;
        }
        return true;
    }

    public static void main(String[] args) {

        validateEmail("info@jsupport.com");

    }
}

1 comments:

troccola said...

Note that the documentation states:

" The current implementation checks many, but not all, syntax rules."

For example, the e-mail a@a is deemed valid.

An attempt to mail to a@a using Gmail results in the following error message: "The email address "a@a" is not recognized. Please fix it and try again."

Microsoft Outlook also responds with an error when sending mail to that address.

Post a Comment