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.
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 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: Salesforce Admin VS Developer - Which one to Choose?
Methods in outbound email.
- SingleEmailMessage: Messagigng.SingleMessage mail = new Messaging.SingleEmailMessage ();
- MassEmailMessage: Messagigng.massMessage mail = new Messaging.massEmailMessage();
- 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[]{'ganesh.maharana@janbask.com'};
string []ccAdd=new string[]{'subham.singh@janbask.com'};
email1.setToAddresses(toAdd);
email1.setCcAddresses(ccAdd);
email1.setSubject('Greeting from Janbask Training');
email1.setPlainTextBody('Janbask Training Career counselling');
email1.setSenderDisplayName('JanaBask Training');
messaging.Email[] emails = new messaging.Email[]{email1};
messaging.sendEmail(emails);
}
}
Output 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 Important is to Know About the Map in Salesforce in 2023?
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)
{
ids.add(c.id);
}
EmailTemplate tem=[Select id,name from EmailTemplate where name = 'Sales: New Customer Email' limit 1];
Messaging.MassEmailMessage Email = new Messaging.MassEmailMessage();
Email.setTargetObjectIds(ids);
Email.setSenderDisplayName('System Admin');
Email.setTemplateId(tem.id);
Messaging.sendEmail(new Messaging.MassEmailMessage[] { Email });
system.debug(Email);
}
}
Run Code
massEmailExamplevv = new massEmailExample();
vv.sendEmail();
system.debug(vv);
Output
Learn Salesforce in the Easiest Way
- Learn from the videos
- Learn anytime anywhere
- Pocket-friendly mode of learning
- Complimentary eBook available
SingleEmailMessage Vs MassEmailMessage|
|
SINGLE EMAIL |
MASS EMAIL |
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: Salesforce Marketing Cloud: A Powerful Marketing Platform
Introduction
Salesforce Vs. Different Technologies
Interview
FaceBook
Twitter
LinkedIn
Pinterest
Email
Salesforce Course
Upcoming Batches
Trending Courses
Cyber Security
- Introduction to cybersecurity
- Cryptography and Secure Communication
- Cloud Computing Architectural Framework
- Security Architectures and Models
Upcoming Class
-0 day 13 Sep 2024
QA
- Introduction and Software Testing
- Software Test Life Cycle
- Automation Testing and API Testing
- Selenium framework development using Testing
Upcoming Class
-0 day 13 Sep 2024
Salesforce
- Salesforce Configuration Introduction
- Security & Automation Process
- Sales & Service Cloud
- Apex Programming, SOQL & SOSL
Upcoming Class
8 days 21 Sep 2024
Business Analyst
- BA & Stakeholders Overview
- BPMN, Requirement Elicitation
- BA Tools & Design Documents
- Enterprise Analysis, Agile & Scrum
Upcoming Class
-0 day 13 Sep 2024
MS SQL Server
- Introduction & Database Query
- Programming, Indexes & System Functions
- SSIS Package Development Procedures
- SSRS Report Design
Upcoming Class
7 days 20 Sep 2024
Data Science
- Data Science Introduction
- Hadoop and Spark Overview
- Python & Intro to R Programming
- Machine Learning
Upcoming Class
-0 day 13 Sep 2024
DevOps
- Intro to DevOps
- GIT and Maven
- Jenkins & Ansible
- Docker and Cloud Computing
Upcoming Class
6 days 19 Sep 2024
Hadoop
- Architecture, HDFS & MapReduce
- Unix Shell & Apache Pig Installation
- HIVE Installation & User-Defined Functions
- SQOOP & Hbase Installation
Upcoming Class
-0 day 13 Sep 2024
Python
- Features of Python
- Python Editors and IDEs
- Data types and Variables
- Python File Operation
Upcoming Class
15 days 28 Sep 2024
Artificial Intelligence
- Components of AI
- Categories of Machine Learning
- Recurrent Neural Networks
- Recurrent Neural Networks
Upcoming Class
8 days 21 Sep 2024
Machine Learning
- Introduction to Machine Learning & Python
- Machine Learning: Supervised Learning
- Machine Learning: Unsupervised Learning
Upcoming Class
21 days 04 Oct 2024
Tableau
- Introduction to Tableau Desktop
- Data Transformation Methods
- Configuring tableau server
- Integration with R & Hadoop
Upcoming Class
-0 day 13 Sep 2024
Salesforce Course
Upcoming Batches
Receive Latest Materials and Offers on Salesforce Course