Skip to content

Instantly share code, notes, and snippets.

@DataSolveProblems
Created September 13, 2019 02:05
Show Gist options
  • Save DataSolveProblems/88224283d7bdc2deb8a3a1b5cb014322 to your computer and use it in GitHub Desktop.
Save DataSolveProblems/88224283d7bdc2deb8a3a1b5cb014322 to your computer and use it in GitHub Desktop.
public class EmailMissionSpecialist {
// Public method
public void sendMail(String address, String subject, String body) {
// Create an email message object
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {address};
mail.setToAddresses(toAddresses);
mail.setSubject(subject);
mail.setPlainTextBody(body);
// Pass this email message to the built-in sendEmail method
// of the Messaging class
Messaging.SendEmailResult[] results = Messaging.sendEmail(
new Messaging.SingleEmailMessage[] { mail });
// Call a helper method to inspect the returned results
inspectResults(results);
}
// Helper method
private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
Boolean sendResult = true;
// sendEmail returns an array of result objects.
// Iterate through the list to inspect results.
// In this class, the methods send only one email,
// so we should have only one result.
for (Messaging.SendEmailResult res : results) {
if (res.isSuccess()) {
System.debug('Email sent successfully');
}
else {
sendResult = false;
System.debug('The following errors occurred: ' + res.getErrors());
}
}
return sendResult;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment