I am working on email Validation using java having RFC 2821 in mind. I have used following code to validate all my email address:
InternetAddress emailAddr = new InternetAddress(email);
emailAddr.validate();
Java api says its RFC-822 compliant. Is there a much difference between RFC 2821 and 822?
Also the above api is failing to validate email in below cases:
var@yahoo- validation returns true, but it is invalid emailvar(comment)@yahoo.com- validation returns false, but it is valid email
Can you tell me any work around for this to get it done.
There are major differences between how modern e-mail addresses are handled vs the e-mail addresses in the original standards.
From what I've experienced setting up dns & bind addresses you can specify a domain name without a period, but when the resolver is queried it will add the
.to the end of the domain name. You can also specify a straight mapping in a hosts file. Most hosts files contain resolve localhost like this:Meaning if you're on the server with the mail server you can send a valid e-mail to
user@localhost.According to RFC 822:
In the case of
[email protected]on local systems as long as the email system is configured properly you can send emails touser@host. Even though this isn't the FQDN - Fully Qualified Domain Name that we're used to now, that standard didn't come around until much later. The mail system then uses the alias to send it to the correct local network translating the email to[email protected]. The problems with e-mail spoofing didn't come around until later when the ARPAnet became public.About the comments in the address, that was not in RFC 822. According to the later email specification which allows comments (RFC 2822 Section 3.4):
Meaning older systems do not allow comments in addresses. RFC 822 does not mention comments in the e-mail address.
The technical fix would be to not allow comments in the e-mail address unless you're accommodating them with custom code. You could always update Javamail. Newer implementations accommodate updated RFCs.