4

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()) {
        system.debug(mr.errors);
    }
}

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

1 Answer 1

7

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.

2
  • 1
    What about the attributre errors mentioned in system.debug(mr.errors);? Is this documented somewhere?
    – Samir
    Commented Mar 10, 2020 at 18:11
  • 3
    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
    Commented Mar 10, 2020 at 18:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .