Send An Email to User and Public Group Using Apex in Salesforce

In Salesforce, we have a flow automation feature that allows us to automate processes such as record updates, task assignments, creation of related records, and sending automated emails.

However, we can use Salesforce Apex when the logic is complex or when working with a large number of records. Triggers are written in code, which provides more control and better performance.

In this tutorial, we will learn how to send an email to user and public group using Apex in Salesforce.

Messaging.SingleEmailMessage Class in Apex

To send an email from Salesforce Apex, we need to use the Messaging.SingleEmailMessage class, which is part of Salesforce’s messaging feature and is specifically used to construct and send single email messages within Salesforce.

This class allows us to customize email properties, including recipients, subject, body, and attachments, and send emails to internal Salesforce users or external addresses.

Methods Used to Send Email in Salesforce Apex

Now let’s see some methods used in Messaging.SingleEmailMessage class in Apex that is used to assign values to emails.

1. setTargetObjectId() Method

In Salesforce Apex, the setTargetObjectId() method in the Messaging.SingleEmailMessage class is used to specify the recipient of an email message. This method identifies a Salesforce user, lead, or contact who will receive the email.

When we set the target object ID, Salesforce automatically pulls in the email address associated with that object. Also, we can retrieve the field values dynamically to add in the email template based on that specific record.

Syntax: Declaring the setTargetObjectId() Method

Using an instance of Messaging.SingleEmailMessage class, we can use the setTargetObjectId() method to add an object from Salesforce from which we want to retrieve values, fields, or email addresses.

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();          email.setTargetObjectId ( targetObjectId );

2. setToAddress() Method

The setToAddress() method is used to set the recipient email addresses for an email we send using the Messaging class. It does not require the recipients to be Salesforce records.

The setToAddress() method is commonly used in Messaging.SingleEmailMessage or Messaging.MassEmailMessage classes are designed to facilitate sending emails using the Apex code.

Syntax: Declaring the setToAddress() Method

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses ( List<String> EmailAddresses );

3. setSubject() Method

In Salesforce Apex, the setSubject() method is used to set the subject line for an email message. This method takes a single parameter, a string representing the subject line of the email, and assigns it to the email message being created. This subject will appear as the email’s subject line when the email is sent.

Syntax: Declaring the setSubject() Method

emailMessage.setSubject ( String subject );   //OR

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessag