Activity Forums Salesforce® Discussions What is the use of optOutPolicy while sending email using apex through SingleEmailMessage()?

  • madhulika shah

    Member
    August 21, 2018 at 7:17 am

    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.

  • shariq

    Member
    September 16, 2018 at 1:01 pm

    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[] { '[email protected]', '[email protected]' };
    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.

  • Parul

    Member
    September 21, 2018 at 1:48 pm

    It helps a lot.

     

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos