aspose file tools*
The moose likes Beginning Java and the fly likes Catching invalid email Id exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Catching invalid email Id exception" Watch "Catching invalid email Id exception" New topic
Author

Catching invalid email Id exception

Vinayak patil
Ranch Hand

Joined: Aug 30, 2005
Posts: 67
Hi,

We are doing a project where we send mails to the ID entered by the user.
We need to catch an exception when the mail id in the "TO" field is
invalid.

We have written the following code:



The code is supposed to catch MessagingException when the mail ID is invalid (i.e the format of mail Id is correct, but email id does not exist like abc@yahoo). But it does not catch any exceptions and returns 1.

Please let me know what are we doing wrong?

Thanks in advance,

Vinayak


"I can resist everything except temptation"
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

I don't understand -- where do you think the exception will come from? The JavaMail API has no way to find out if an email address is "real" except by attempting to deliver the message. Even if the message is accepted by the local MTA, that's no assurance that it won't eventually be rejected. Finding out whether a given email address is valid is, in the general case, a tough problem, and JavaMail isn't going to just hand you the answer.


[Jess in Action][AskingGoodQuestions]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5


The code is supposed to catch MessagingException when the mail ID is invalid (i.e the format of mail Id is correct, but email id does not exist like abc@yahoo). But it does not catch any exceptions and returns 1.


A common practice in simple validation of e-mail is to ensure that there exists a . character after an '@' so is it very easy to check that abc@yahoo is invalid. Although checking abc@yahoo.cim is invalid is much harder for reasons all ready listed. Even if a mail address is valid at the time of sending it may be deleted and become invalid before it is delivered since messaging is inherently asynchronus.


My Blog: Down Home Country Coding with Scott Selikoff
Vinayak patil
Ranch Hand

Joined: Aug 30, 2005
Posts: 67
We had written a code, that got us the following:

It is part of the printstacktrace.



Wrong To address
-----------------
javax.servlet.ServletException: Sending failed;
nested exception is:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
javax.mail.SendFailedException: 550 5.7.1 Unable to relay for vinayak.n.patil@abc.com

Root Cause
----------
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
javax.mail.SendFailedException: 550 5.7.1 Unable to relay for vinayak.n.patil@abc.com


Earlier we used to get this exception. We changed the code and are not getting the above result. We are not able to track the changes and so are unable to point the exact error.

-Vinayak
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Since you're not getting any error now, you need to do some more investigation but I did find one post about this on Sun's forums that suggested the problem is with the SMTP server you are sending with:

Sun Forum link
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It's been a while since I worked with javax.mail, but I note that Transport allows you to addTransportListener() - it looks like this is supposed to provide notification in case of delivery failure. I suspect it is not guaranteed, since for many types of delivery failure it would have to depend on the receiving server to send a bounce message in some standard format; I don't believe you can be sure this will happen. But it's probably worthwhile to try registering a listener to see what notifications you can receive.

SendFailedException and related issues are also dicussed in Sun's JavaMail FAQ. Good luck.
[ October 25, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
 
jQuery in Action, 2nd edition
 
subject: Catching invalid email Id exception