Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JavaMail: Checking for valid e-mail address  RSS feed

 
Tiago Fernandez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey fellas,

I need to check if a String is a valid e-mail address. Does the code bellow makes sense for you?



Tks
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JavaMail API:

Parse the given sequence of addresses into InternetAddress objects. If strict is false, simple email addresses separated by spaces are also allowed. If strict is true, many (but not all) of the RFC822 syntax rules are enforced. In particular, even if strict is true, addresses composed of simple names (with no "@domain" part) are allowed. Such "illegal" addresses are not uncommon in real messages.

Non-strict parsing is typically used when parsing a list of mail addresses entered by a human. Strict parsing is typically used when parsing address headers in mail messages.


I would say that you method might work sometimes. A better method would be to use a Regular Expression somewhere between getting the email address and handing it to the JavaMail API. That way you ensure that all the addresses are correct as soon as possible.

Here is Sun's tutorial on RE's. If you have other RE questions the Java In General (Intermediate) is the most appropriate forum for those. Here is a recent post on that very topic.
[ July 01, 2004: Message edited by: Gregg Bolinger ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!