+ Start a Discussion
asasiasasi 

How to handle Invalid email from batch apex class

Dear all,

 

I have a requirement to implement the bulk email sending process related to my order module. We implemented a solution through batch apex class which will send on hourly basis all newly created orders and send to concern person.

 

We are facing an issue like when the email ID (which is stored in Account object) is invalid (say: 12345@as.com), we should send an invalid email notification to the user who created the order.

 

How do we catch the invalid email issue in scheduled batch apex class? We tried try and catch block with different exceptions like email exception etc... but no result.

 

Can somebody provide a solution for the same? Responses would be appreciated.

 

Thanks

Asish

kiranmutturukiranmutturu

i hope you are sending the email from apex then we have 

Messaging.SendEmailResult and  SendEmailError objects to get the error details .

 

so u can make use of this to get it done.

Vinit_KumarVinit_Kumar

As informed by Kiran use Messaging.SendEmailResult and for that you can use the below code :-

 

List<Messaging.SendEmailResult> results = 

Messaging.sendEmail(new Messaging.Email[] { email });
if (!results.get(0).isSuccess()) {
System.StatusCode statusCode = results.get(0).getErrors()[0].getStatusCode();
String errorMessage = results.get(0).getErrors()[0].getMessage();
}

 

for more on this class ,try below link :-

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_sendemail_emailresult.htm

asasiasasi

Hi Vinit/Kiran,

 

Thanks for your quick reponse

We tried the SendEmailError as well. The issue is like our messaging itself is a list as shown below.

 

List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();

 

In that case if I pass mailLits to SendEmailError it will not return anything. If I loop through the list and pass it then how I can get the collection of error.

 

Thanks

Asish