Today's Offer - Salesforce Certification Training - Enroll at Flat 10% Off.

- 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 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 Serviceto 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 an existing email service.
  • Click Delete to delete an email service.

Salesforce uses two types of email service

Read: Salesforce Lightning Interview Questions and Answers
  • Outbound email
  • Inbound email

Outbound Email

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

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.

Read: A Basic Overview for Salesforce Security
  • 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.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);
    }
}

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 users 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


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 What Apex Email in Salesforce?

Read: Salesforce Apex Interview Questions and Answers

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

Salesforce Tutorial Overview


    Janbask Training

    JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.


Trending Courses

AWS

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

Upcoming Class

1 day 14 Dec 2019

DevOps

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

Upcoming Class

2 days 15 Dec 2019

Data Science

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

Upcoming Class

2 days 15 Dec 2019

Hadoop

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

Upcoming Class

3 days 16 Dec 2019

Salesforce

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

Upcoming Class

3 days 16 Dec 2019

QA

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

Upcoming Class

7 days 20 Dec 2019

Business Analyst

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

Upcoming Class

3 days 16 Dec 2019

SQL Server

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

Upcoming Class

3 days 16 Dec 2019

Comments

Search Posts

Reset

Receive Latest Materials and Offers on Salesforce Course

Interviews