Activity › Forums › Salesforce® Discussions › What is the use of optOutPolicy while sending email using apex through SingleEmailMessage()?
Tagged: Apex, optOut Policy, sendEmail, SingleEmailMessage
-
What is the use of optOutPolicy while sending email using apex through SingleEmailMessage()?
Posted by Parul on August 21, 2018 at 7:13 AMWhat is the use of optOutPolicy while sending email using apex through SingleEmailMessage()?
Parul replied 7 years, 7 months ago 3 Members · 3 Replies -
3 Replies
-
Hi Parul,
It is Optional. If you added recipients by ID instead of email address and the Email Opt Out option is set, this method determines the behavior of the sendEmail() call. If you add recipients by their email addresses, the opt-out settings for those recipients aren’t checked and those recipients always receive the email.
- [adinserter block='9']
-
Hi,
To Make it more simple to understand-
If Opt Out Policy is set in single email then no matter what your recipients will always receive the mail.
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { ‘abc2@gmail.com’, ‘abc2@gmail.com’ };
message.optOutPolicy = ‘FILTER’;
message.subject = ‘Subject Test Message’;
message.plainTextBody = ‘This is the message body.’;
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);if (results[0].success) {
System.debug(‘The email was sent successfully.’);
} else {
System.debug(‘The email failed to send: ‘ + results[0].errors[0].message);
}Hope this helps.
Log In to reply.