This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Validate email address using InternetAddress Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Validate email address using InternetAddress" Watch "Validate email address using InternetAddress" New topic
Author

Validate email address using InternetAddress

Cole Terry
Ranch Hand

Joined: Nov 23, 2011
Posts: 45
Hi guys,

After looking at the threads:

- http://www.coderanch.com/t/499606/java/java/Email-regEx-should-not-start#2251319
- http://www.coderanch.com/t/385474/java/java/Email-Validation-code#2448132

I decided to use InternetAddress class to validate the email address instead of using regular expression in my code.
Wondering if the following is a correct usage:




Any suggestions are warmly welcome!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

After reading the API documentation, I would have to say... I don't know. The expression

is equivalent to

in which "the detailed syntax of the address isn't checked", or so says the documentation. On the other hand the documentation also says "The address is parsed using "strict" parsing". So you might want to experiment a bit with those constructors to see what they actually do. However I'm dubious of the value of validating e-mail addresses anyway so I wouldn't (and didn't) worry about it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Line 1 of your little code block can also throw an AddressException. You'll need to catch that as well. I usually just use the constructor with strict parsing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Cole Terry
Ranch Hand

Joined: Nov 23, 2011
Posts: 45
Thanks Paul and Rob.

So I think I may use this form:




However one issue is, if I use this class to validate the email address only (do use other features of Java Mail API), I have to use the mail.jar which adds additional size to my application.
So I think for this case, it's better to use a simple regular expression for the validation.

What do you think guys?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

There is no proper simple regular expression for email addresses. Either it's incomplete, or it's complex.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

The mail.jar file is very small, as jar files go these days. But I'm puzzled about why you are collecting e-mail addresses but don't plan to do anything with them.
Cole Terry
Ranch Hand

Joined: Nov 23, 2011
Posts: 45
Paul Clapham wrote:The mail.jar file is very small, as jar files go these days. But I'm puzzled about why you are collecting e-mail addresses but don't plan to do anything with them.


For example, for validating a registration form that contains an email field, but do not send any email out.
Juan Carlos Arias
Greenhorn

Joined: Dec 19, 2011
Posts: 1
How deep are you wanting to validate the email address? Syntax is one thing, but if you want to check whether the domain and/or email address actually exists then you have to do a bit more work. See the following class:

http://files.jscape.com/secureinetfactory/docs/html/overview15.html



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validate email address using InternetAddress