Our Support: During the COVID-19 outbreak, we request learners to CALL US for Special Discounts!

- Salesforce Blogs -

What Apex Email in Salesforce?

What Apex Email in Salesforce?

You can use email services to process the contents, headers, and attachments to the inbound email. For example, you can create an email service that automatically creates contact records based on the contact information in messages. Email service is using Apex classes to process the contents, headers, and attachments of the email automatically. You can associate each email service with one or more Salesforce-generated email addresses to which users can send messages for processing. The general template to create the apex class for the email services is:

To give multiple users access to a single email service, you can:

  • Associate multiple Salesforce-generated emails address with the email service and allocate those addresses to users.
  • Write an Apex class that executes a single Salesforce-generated email address according to the user accessing the email service address with the email service. For example, you can write an Apex class that identifies the user based on the user's email address and creates records on behalf of that user.

salesforce Curriculum To use email services, from Setup, enter Email Services in the Quick Find box, then select Email Services.

  • Click New Email Service to define a new email service.
  • Select an existing email service to view its configuration, activate or deactivate it, and view or specify addresses for that email service.
  • Click Edit to make changes to the existing email service.
  • Click Delete to delete an email service.

Salesforce Training For Administrators & Developers

Read: How Can I Start my Career in the Salesforce Industry?
  • No cost for a Demo Class
  • Industry Expert as your Trainer
  • Available as per your schedule
  • Customer Support Available

Salesforce uses two types of email service

Salesforce uses two types of email service

Outbound Email

Use Apex to work with an email sent from Salesforce. You can use Apex to send individual and mass emails. We can send Salesforce email templates and be in plain text or HTML format, or those generated by Visualforce.

Read: What is Salesforce Sandbox? How to Create & Uses of Sandbox?

Methods in outbound email.

  1. SingleEmailMessage: Messagigng.SingleMessage mail = new Messaging.SingleEmailMessage ();
  2. MassEmailMessage: Messagigng.massMessage mail = new Messaging.massEmailMessage();
  3. Messaging: Includes the static sendEmail method, which sends the email objects you instantiate with either the SingleEmailMessageor MassEmailMessage classes, and returns

aSendEmailResult object. The syntax for sending an email is: Messaging.sendEmail(new Messaging.Email[]{mail},opt_allOrNone);

SingleEmailMessage Methods

The following are methods for SingleEmailMessage. All are instance methods.

  • setBccAddresses(bccAddresses) A list of blind carbon copy (BCC) addresses or object IDs of the contacts, leads, and users you’re sending the email to. The maximum allowed is 25.
  • setCcAddresses(ccAddresses) A list of carbon copy (CC) addresses or object IDs of the contacts, leads, and users you’re sending the email to. The maximum allowed is 25.
  • setPlainTextBody(plainTextBody) The text version of the email, specified by the sender. You must specify a value for setTemplateId,setHtmlBody, or setPlainTextBody. Or, you can define both setHtmlBody and setPlainTextBody.
  • setSubject(subject) The email subject line. If you are using an email template, the subject line of the template overrides this value.
  • setToAddresses(toAddresses) A list of email addresses or object IDs of the contacts, leads, and users you’re sending the email to. The maximum number of email addresses allowed is 100.
  • setFileAttachments(fileNames) A list containing the file names of the binary and text files you want to attach to the email.
  • setHtmlBody(htmlBody) The HTML version of the email, specified by the sender. The value is encoded according to the specification associated with the organization. You must specify a value for setTemplateId, setHtmlBody, or setPlainTextBody. Or, you can define both setHtmlBody and setPlainTextBody.

public class outboundemail {
public void myEmail(){
messaging.SingleEmailMessage email1=new messaging.SingleEmailMessage();
string[]toAdd=new string[]{'[email protected]'};
string []ccAdd=new string[]{'[email protected]'};
email1.setSubject('Greeting from Janbask Training');
email1.setPlainTextBody('Janbask Training Career counselling');
email1.setSenderDisplayName('JanaBask Training');
messaging.Email[] emails = new messaging.Email[]{email1};

What Apex Email in Salesforce? Output What Apex Email in Salesforce?   Mass EmailMessage We can send a mass email message to a recipient list consists of contact, lead, person’s account or user account are available in Salesforce. Messaging.massEmailMessageMethods: This method consists of three methods.

  • setDescription(description) The description of the email.
  • setTargetObjectIds(targetObjectIds) A list of IDs of the contacts, leads, or users to which the email will be sent. The IDs you specify set the context and ensure that merge fields in the template contain the correct data. The target objects must be typeof contacts, all leads, or all users.
  • setWhatIds(whatIds) This is optional. If you specify a list of contacts for the targetObjectIdsfield, you can specify a list of whatIds as well. This helps to further ensure that merge fields in the template contain the correct data.

Example of MassEmail

Read: How to Create Assignment Rules in Salesforce? [Updated in 2020]

public class massEmailExample {
public void SendEmail()
 List<contact>conlist=[Select id ,lastName from contact where lastNamelike'g%' ];
 List<Id> ids= new List<Id>();
for(Contact c:conlist)
EmailTemplate tem=[Select id,name from EmailTemplate where name = 'Sales: New Customer Email' limit 1];
Messaging.MassEmailMessage Email = new Messaging.MassEmailMessage();
Email.setSenderDisplayName('System Admin');
Messaging.sendEmail(new Messaging.MassEmailMessage[] { Email });
Run Code
massEmailExamplevv = new massEmailExample();

Output What Apex Email in Salesforce?

Learn Salesforce in the Easiest Way

  • Learn from the videos
  • Learn anytime anywhere
  • Pocket-friendly mode of learning
  • Complimentary eBook available

SingleEmailMessage Vs MassEmailMessage|

Multiple recipients? Yes Yes
Personalized body? Yes (single body only) Yes
Special permission needed? No Yes, has to be enabled
Merge fields? Yes Yes
Personalized merge fields? Yes (only one record at a time) Yes
Templates? Yes Yes
Template possibilities? Text/HTML/Visualforce/Custom Templates Text/HTML/Custom Template

Read: How to Integrate Salesforce with Outlook, Gmail, & Mailchimp

Salesforce Tutorial Overview

    Janbask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.


Trending Courses


  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

1 day 21 Sep 2020


  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

6 days 26 Sep 2020

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

4 days 24 Sep 2020


  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

19 days 09 Oct 2020


  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

1 day 21 Sep 2020


  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

10 days 30 Sep 2020

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

5 days 25 Sep 2020

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

5 days 25 Sep 2020


  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation

Upcoming Class

9 days 29 Sep 2020

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks

Upcoming Class

4 days 24 Sep 2020

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning

Upcoming Class

7 days 27 Sep 2020


  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop

Upcoming Class

5 days 25 Sep 2020

Search Posts


Receive Latest Materials and Offers on Salesforce Course