+ Start a Discussion
narsavagepnarsavagep 

Error attempting to send email to invalid email address -- not being caught by try/catch

I have the following code:  (Note, it is part of a future-call method)

 

 

...
String EmailFailure = null;
	
try {
	Messaging.SendEmailResult[] mailResult = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
	if ( !mailResult[0].isSuccess() ) {
		EmailFailure = 'Email Failed: ' + mailResult[0].getErrors()[0].getMessage();
	}
}
catch(DmlException dmlEx) {
	EmailFailure = 'Email Failed: ' + dmlEx;
}
	
if ( EmailFailure != null ) {
...

 

 

It is failing with the following error whenever an attempt is made to send to a contact with an invalid (bounced) email address.

 

Failed to invoke future method 'public static void SendSurvey(String, Id, Id, Id, Id, Id, String, String)' on class 'Survey_Controller' for job id '70730000003xhE1'

caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, email address has bounced for id : 0033000000VqJyN: []

 

 

How can I capture the error gracefully instead of getting this error message?  I thought if I wrapped it with try/catch it would work.

 

Thanks,
Paul

Best Answer chosen by Admin (Salesforce Developers) 
jhurstjhurst

Paul,

 

Can you try to catch the EMail exception:

 

 

...
String EmailFailure = null;
	
try {
	Messaging.SendEmailResult[] mailResult = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
	if ( !mailResult[0].isSuccess() ) {
		EmailFailure = 'Email Failed: ' + mailResult[0].getErrors()[0].getMessage();
	}
}
catch(DmlException dmlEx) {
	EmailFailure = 'Email Failed: ' + dmlEx;
}
catch(System.EmailException emlEx) {
	EmailFailure = 'Email Failed: ' + emlEx;
}
	
if ( EmailFailure != null ) {
...

 

Exception types and methods can be found at 

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_classes_exception_methods.htm?SearchType=Stem.

 

Hope this helps.

Jay

All Answers

jhurstjhurst

Paul,

 

Can you try to catch the EMail exception:

 

 

...
String EmailFailure = null;
	
try {
	Messaging.SendEmailResult[] mailResult = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
	if ( !mailResult[0].isSuccess() ) {
		EmailFailure = 'Email Failed: ' + mailResult[0].getErrors()[0].getMessage();
	}
}
catch(DmlException dmlEx) {
	EmailFailure = 'Email Failed: ' + dmlEx;
}
catch(System.EmailException emlEx) {
	EmailFailure = 'Email Failed: ' + emlEx;
}
	
if ( EmailFailure != null ) {
...

 

Exception types and methods can be found at 

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_classes_exception_methods.htm?SearchType=Stem.

 

Hope this helps.

Jay

This was selected as the best answer
narsavagepnarsavagep

I had been using an "example" of exception catching, and it had been working for me in test methods (for catching DML errors).  I hadn't even realized that error catching was specific to the exception type being thrown.  Thanks for this reply, I was able to fix my code to appropriately catch the invalid-email errors now.

 

Thanks!

Paul

suresh.csksuresh.csk

Hi.

 

I tried your method not working.

 

List<Messaging.SendEmailResult> results = new List<Messaging.SendEmailResult>{};
try{
        results = Messaging.sendEmail(allMails);
 }catch(EmailException emlEx) {
        msg = String.valueof('msg:' + emlEx);
        System.debug(msg);
}

 

I sending a bunch of mails,so is there any time delay  from email server which responses to SFDC server.

cheers

suresh

 

jhurstjhurst

There should be no time delay in sending.

 

What is the error that you are receiving?  You should try an catch all of the different exceptions.  Can you add a catch for the DML exception?

 

From your code snip, I cannot really tell what is happening (what is allMails).  What happens if you use the sample code above?

 

Thanks

Jay

Sakthi RSakthi R
jhurst i've tried the logic given by you here. its not working. i've given  the email like abc@abc.com this.. this doesnt shows error rather sends email to this id and counts the limt.. any help for this??