Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validate email address using InternetAddress

 
Cole Terry
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cole Terry
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no proper simple regular expression for email addresses. Either it's incomplete, or it's complex.
 
Paul Clapham
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic