User Story
Create a Apex Program which sends emails with parameters
Parameters
Expected Outcome
Email sent successfully
or
The following errors occurred: ....
Code
public class EmailManager {
public void sendMail(String address, String subject, String body) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {address};
mail.setToAddresses(toAddresses);
mail.setSubject(subject);
mail.setPlainTextBody(body);
Messaging.SendEmailResult[] results = Messaging.sendEmail(
new Messaging.SingleEmailMessage[] { mail });
inspectResults(results);
}
private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
Boolean sendResult = true;
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;
}
}
Execute the Code to Send and Email
EmailManager em = new EmailManager();
em.sendMail('Your email address', 'Trailhead Tutorial', '123 body');
Video
Video does not exists.