Grab Deal : Flat 30% off on live classes + 2 free self-paced courses! - SCHEDULE CALL

- 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 to Update, Insert, Export or Delete Data Using Apex Data Loader?
  • 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: Queueable Apex Job

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[]{''};
string []ccAdd=new string[]{''};
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: The Comprehensive Salesforce Tutorial: Learn Salesforce and Upskill in Just 15 Ways!

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: An Ultimate Guide to Salesforce Developer Certification {2023 & Beyond)

Salesforce Tutorial Overview

fbicons FaceBook twitterTwitter lingedinLinkedIn pinterest Pinterest emailEmail


    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.

  • fb-15
  • twitter-15
  • linkedin-15


Trending Courses

Cyber Security Course

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models
Cyber Security Course

Upcoming Class

16 days 05 Jul 2024

QA Course


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

Upcoming Class

8 days 27 Jun 2024

Salesforce Course


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

Upcoming Class

7 days 26 Jun 2024

Business Analyst Course

Business Analyst

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

Upcoming Class

9 days 28 Jun 2024

MS SQL Server Course

MS SQL Server

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

Upcoming Class

9 days 28 Jun 2024

Data Science Course

Data Science

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

Upcoming Class

2 days 21 Jun 2024

DevOps Course


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

Upcoming Class

5 days 24 Jun 2024

Hadoop Course


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

Upcoming Class

2 days 21 Jun 2024

Python Course


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

Upcoming Class

17 days 06 Jul 2024

Artificial Intelligence Course

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence Course

Upcoming Class

10 days 29 Jun 2024

Machine Learning Course

Machine Learning

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

Upcoming Class

23 days 12 Jul 2024

 Tableau Course


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

Upcoming Class

2 days 21 Jun 2024

Search Posts


Receive Latest Materials and Offers on Salesforce Course