-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEmailManager.cls
39 lines (35 loc) · 1.5 KB
/
EmailManager.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class EmailManager {
//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.setToAddress(toAddresses);
mail.setSubject(subject);
mail.setPlainTextBody(body);
//Pass this email message to the built-in sendMail method of the Messaging class
Messaging.SendEmailResult[] results = Messaging.sendEmail(
new Messaging.SingleEmailMessage[] { mail }
);
//Call a helper method to inspect the returned result
//This helper method inspects the results of the email send call and is called by sendMail()
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 sent 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 occured: ' + res.getErrors());
}
}
return sendResult;
}
}