+ Start a Discussion

Reterieve error message on failure of sendEmail


I want to retrieve the error message if sendEmail failed.

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Strings to hold the email addresses to which you are sending the email.
String[] toAddresses = new String[] {'user@acme.com'};
String[] ccAddresses = new String[] {'smith@gmail.com'};
// Assign the addresses for the To and CC lists to the mail object.
// Specify the address used when the recipients reply to the email.
// Specify the name used as the display name.
mail.setSenderDisplayName('Salesforce Support');
// Specify the subject line for your email address.
mail.setSubject('New Case Created : ' + case.Id);
// Set to True if you want to BCC yourself on the email.
// Optionally append the salesforce.com email signature to the email.
// The email address of the user executing the Apex Code will be used.
// Specify the text content of the email.
mail.setPlainTextBody('Your Case: ' + case.Id +' has been created');
mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created<p>'+
' View case <a href=https://prerelna1.pre.salesforce.com/'+case.Id+'>click here</a>');
// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

Any pointer on this will be helpful.


Best Answer chosen by Admin (Salesforce Developers) 
Look at the Exception class and using exception methods section of the documentation, which describes how to use the exception classes to capture and report error messages.