0

I'm trying to practice exception handling in java, and doing so by running a few (in)valid e-mail addresses and validating them. So far, I'm able to print out the non validated ones, but I'm looking for a way to also print the one who is validated. If I println in the try block, the john.doe.gmail.com address prints out as being validated for some reason.

Could someone suggest something?

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

public class AdressChecker  {
  public static void main(String args[]) {
    String toTest[] =
          {"john.doe@gmail.com", "john.doe.gmail.com", "john.doe@", "@gmail.com"};

    System.out.println("Addresses to be verified");
    for (String x : toTest)
      System.out.println(x);
    for (String s : toTest) {
      try {
        InternetAddress emailAddr = new InternetAddress(s);                    
        emailAddr.validate();                        
      } catch (AddressException ex) {
        System.out.println(s + " is not a valid E-Mail Address.");
      }
    }
  }
}    

Working now. The output is:

Addresses to be verified
john.doe@gmail.com
john.doe.gmail.com
john.doe@
@gmail.com
john.doe@gmail.com is a valid E-Mail Address.
john.doe.gmail.com is not a valid E-Mail Address.
john.doe@ is not a valid E-Mail Address.
@gmail.com is not a valid E-Mail Address.
BUILD SUCCESSFUL (total time: 0 seconds)
Boricode
  • 35
  • 6

3 Answers3

0

You could add your println statement after .validate() call. Since it will raise an exception when the address is invalid, the println call will not be executed.

Guillaume Poussel
  • 9,572
  • 2
  • 33
  • 42
0

Try to print the email after emailAddr.validate() line check this :

for (String s : toTest) 
        {
            try
             {
                 InternetAddress emailAddr = new InternetAddress(s);                    
                 emailAddr.validate();
                 System.out.println(s + " is a valid E-Mail Address.");                        
             }    
            catch (AddressException ex)
             {
             System.out.println(s + " is not a valid E-Mail Address.");
              }

       }
Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90
  • then it might be a problem with java mail API, could you try by using only john.doe.gmail.com address, whether it is validating or throwing any exception. – Ramesh Kotha Oct 13 '13 at 10:26
  • Because it's valid `InternetAddress`. You need other method to check e-mails. For this simple case, regex may do the job: `^\w+(\.\w+){0,1}@\w+\.\w+$` @Boricode – Display Name Oct 13 '13 at 10:28
  • With Java Mail API 1.5, only `john.doe@gmail.com` is marked as valid. No need for a regex. – Guillaume Poussel Oct 13 '13 at 10:32
0

With the InternetAddress.validate() method you do not check the validity of only email addresses. The syntax in RFC 822 (section 6.1) is much broader than the addresses we generally use today as email addresses. Think about tools like ssh that also take internet addresses as parameter and they look like: user@localhost. This passes the InternetAddress validation because it is a valid Internet address. And can be a valid email address as well in certain network scenarios.

So you will have answers like this?

Addresses to be verified
john@gmail is a valid E-Mail Address
john@gmail.coooooooooooooom is a valid E-Mail Address
john+1@gmail.com is a valid E-Mail Address
john+2@gmail.com is a valid E-Mail Address
john+3000@gmail.com is a valid E-Mail Address

It depends very much of your scenarios in your application. Sometimes you want to:

  • avoid aliases (john+1@gmail.com is the same as john@gmail.com for Gmail)
  • avoid non existent top level domains (cooooooom) (until the new generic top level domains really come into existence)
  • etc.

If your email addresses have to be only valid email addresses that are used on the Web (like having domain and subdomain), I recommend using a regular expression. For this you have plenty answers:

Community
  • 1
  • 1
Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168
  • I would recommend using the validator from the [Apache Commons Validator](http://commons.apache.org/proper/commons-validator/) framework. There was only one case that the validator didn't work for, and it is actually explained in the javadoc of the validator. – hooknc Oct 13 '13 at 16:10
  • There is also a [Hibernate Validator](http://www.hibernate.org/subprojects/validator.html) for validating [Email Addresses](http://docs.jboss.org/hibernate/validator/4.2/api/org/hibernate/validator/constraints/impl/EmailValidator.html). – hooknc Oct 13 '13 at 16:16