Thursday 11 May 2017

Send emails to multiple email Ids using Apex.

Send mails using Messaging.SingleEmailMessage to multiple email ids.
Provide email address in list and assign in to toAddress.

Apex Class:

public class sendMail
    public sendMail()
        List<String> emails=new List<String>();
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.toAddresses = emails;
        message.subject = 'Hi Salesforce';
        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);


Post a Comment

If you have any doubts, please let me know.