Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to handle wrong to mail address while sending email.

807588Jul 14 2009 — edited Jul 14 2009
How to validate SMTP check while sending an Email.
		boolean mailSend=true;
		boolean isAttach = false;
		StringBuffer reason = null;
		String str = null;

		MimeMessage message=javaMailSender.createMimeMessage();

		try
		{
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			if(toSend !=null)
				helper.setTo(toSend);
			else
				helper.setTo("abc@gmail.com");
			if(msg !=null)
				helper.setText(msg,true);
			else
				helper.setText("<BR><STRONG>&nbsp;&nbsp; ABC</STRONG></P><BR>", true);
			
			if(fromMsg != null && !fromMsg.equals(""))
				helper.setFrom(fromMsg);
			else
				helper.setFrom("registry@gmail.com"); 
				
			helper.setSubject(subject);
			if(locationVec!=null && locationVec.size()>0)
			{
				FileSystemResource []file=new FileSystemResource[locationVec.size()];
				for (int i=0;i<locationVec.size();i++)
				{
					str="";
					str=(String)locationVec.get(i);
					file[i] = new FileSystemResource(new File(str));
					if(!file.exists())
{
isAttach = true;
throw new Exception("FileNotFoundException");
}
if(file[i]!=null)
{
helper.addAttachment(file[i].getFilename(), file[i]);
}
}
isAttach = true;
}
javaMailSender.send(message);
}
catch(Exception ex)
{
StringBuffer exception = new StringBuffer(ex.getMessage().toString());

if (exception.indexOf("ConnectException") >= 0) // connection problem.
{
reason = new StringBuffer(" Unable to Connect Mail server");
}
else if (exception.indexOf("SendFailedException") >= 0) // Wrong To Address
{
reason = new StringBuffer("Wrong To Mail address");
}
else if (exception.indexOf("FileNotFoundException") >= 0) //File Not Found at Specified Location
{
reason = new StringBuffer("File Not Found at Specific location");
}
else // Email has not been sent.
{
reason = new StringBuffer("Email has not been sent.");
}
LogMessage.debug("Exception occured while sending the mail : " + ex);
LogMessage.debug("Email has not been sent due to one of these reasons :" +
" 1)Invalid email address 2)Attachments in mail may not be found and 3)Connection problem.");
mailSend =false;
if we give "abc@gmail111.com" then it is showing exception "Wrong To Mail address." but if we give "abc@gmail.com" then it is not giving any exception and it shows mail send. but is is non existing mail id. so can u please tell me how to validate SMTP check 
i have a code for this validation but that one is in .NET

http://www.coveryourasp.com/ValidateEmail.asp#Result3

http://www.coveryourasp.com/ShowSource.asp?page=ValidateEmail

please reply.

Thanks,
-Ravi.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 11 2009
Added on Jul 14 2009
19 comments
1,194 views