How To Send Email Using Apex

by | Aug 3, 2022 | Salesforce

Home » Salesforce » How To Send Email Using Apex

Introduction

We can use Apex to send individual and mass emails in Salesforce to include all the standard attributes like subject, display name, reply to, etc. Email can be sent when any field gets updated, or any async operation is completed

What are the options for sending Emails in Salesforce?

We have multiple ways to send email in Salesforce, like through automation tools (Workflow, Process Builder, Flows), Apex classes, etc.

Difference between Single Email and Mass Email Message

Each of the emails in Single Emails has the same body and is very similar to regular individual emails that may get delivered to one or more email addresses (to/bcc/cc). In contrast, each of the emails in Mass Emails has personalized message bodies that go to many addresses.

We can make use of various templates for single emails. We have Text/HTML/Visualforce/Custom Templates, whereas, for mass emails, we can make use of Text/HTML/Custom Templates.

How to send Single Email using APEX (with Example)

public class SendEmail {
public void emailMethod(){
Messaging.SingleEmailMessage sm = new Messaging.SingleEmailMessage();
String[] toAdd = new String[]{'xyz@gmail.com'};
String[] ccAdd = new String[]{'abc@gmail.com'};
sm.setToaddresses(toAdd);
sm.setCCaddresses(ccAdd);
sm.setSubject('This is my email subject');
sm.setPlainTextBody('Welcome to the automated email event!!');
sm.setSenderDisplayName('XYZ Sender');
// to send mail
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{sm});
}
}

 

SingleEmailMessage instantiates an email object through which we send a single email message.

Messaging.SingleEmailMessage sm = new Messaging.SingleEmailMessage();

To send a single email message, we have to create an object for SingleEmailMessage.

We have been provided with methods to help us draft and send emails from Salesforce.

Some are listed below:

1. setToaddresses(): set the address. It can be up to 100

addresses

2. setCCaddresses(): set the cc address to whom an email

should be sent

3. setSubject(string): set the subject of the mail

sm.setSubject(‘This is my email subject’);

4. setPlainTextBody(): set the main body of the email in plain

text sm.setPlainTextBody(‘Welcome to the automated email event!!’);

How to Send Mass Email using APEX (with Example)

public class MassSendEmail {
List accList = [SELECT Id, Name FROM Account LIMIT 5];
List accIds = new List();
for(Account acc : accList){
accIds.add(acc.Id);
}
EmailTemplate eTemp = [SELECT Id FROM EmailTemplate WHERE Name = 'Account Email Temp Name' LIMIT 1];
Messaging.MassEmailMessage met = new Messaging.MassEmailMessage();
met.setTargetObjectIds(accIds);
met.setSenderDisplayName('System Admin Message');
met.setTemplateId(eTemp.Id);
Messaging.sendEmail(new Messaging.MassEmailMessage[] { met });
}

 

Methods for mass email messaging are:

1. setTargetObjectIds(targetObjectIds): We can specify upto 250 ids per email as a target object ids.

met.setTargetObjectIds(accIds);

2. setTemplateId(tempIds): It is used to set the template ids.

met.setTemplateId(eTemp.Id);

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author