This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Catching invalid email Id exception

 
Vinayak patil
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4010
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Vinayak patil
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Saloon Keeper
Posts: 4010
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic