Send email from Sal...
 
Notifications
Clear all

[Solved] Send email from Salesforce apex to system admins

2 Posts
1 Users
0 Likes
340 Views
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

What would be the simplest way to send emails alerts to Salesforce system admins from apex?

Issue Tags
1 Reply
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

Try this:

        List<User> userList = new List<User>();
        userList = [SELECT Email FROM User WHERE Profile.Name = 'System Administrator' AND IsActive = True] ;


        List<String> toAddresses = new List<String>();
        for (User u : userList) {           
            toAddresses.add(u.Email);
        }
        // System.debug('toAddresses: ' + toAddresses); 


        String messageBody = 'message body here ';
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.toAddresses = toAddresses;
        message.subject = 'subject here';
        message.plainTextBody = messageBody;
        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);
        }
Reply
Share: