I'm sending multiple emails using Messaging.sendEmail. After that I'm checking for the results by looping over the List<Messaging.SendEmailResult> just like the example below. Now my question is: what does the errors attribute contain? I tried to trigger send email error but it wasn't successful. I need to know if I can use the errors attribute to get the email address of the ones that failed or somehow find out which email from the list of emails failed. Thank you!

for (Messaging.SendEmailResult mr : mrs) 
    if (!mr.isSuccess()) {

I've also checked the following apex class documentation SendEmailError Class but didn't find anything that could help.


errrors is a list of SendEmailError. You can Iterate and collect errors:

for (Messaging.SendEmailResult mr : mrs) 
    if (mr.isSuccess()) {
        //Do something for success
    }  else {
        // Operation failed, so get all errors                
        for(Messaging.SendEmailError err : mr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('fields that affected this error: ' + err.getFields());

See methods for SendEmailError here.

| improve this answer | |
  • 1
    What about the attributre errors mentioned in system.debug(mr.errors);? Is this documented somewhere? – Samir Mar 10 at 18:11
  • 2
    errors is a list of SendEmailError, you can access it directly or via using the getter getErrors(). It is better to use documented methods because moving forward this attribute can be made private. – manjit5190 Mar 10 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.