Email Programming in Salesforce with Apex: A Complete Guide

Email Programming in Salesforce with Apex: A Complete Guide 

 

 

Email notifications play a crucial role in business processes, ensuring timely communication with customers and stakeholders. While Salesforce provides workflow email alerts, there are scenarios where Apex-based email programming is the best option. This article explores how to send and receive emails using Apex, including handling attachments and adhering to governor limits. 

 

## Why Use Apex for Email Programming? 

 

Sometimes, workflow email alerts are not sufficient due to complex criteria or dynamically determined recipients. In such cases, Apex allows for more flexibility and customization. Salesforce enables sending email notifications to users or external systems and receiving emails from users, all while supporting attachments. 

 

## Types of Email Services in Salesforce 

 

Salesforce provides two types of email services: 

 

  1. *Inbound Email Messaging* – Used to receive emails from users or external systems.
  2. *Outbound Email Messaging* – Used to send emails to users with specific recipients, subjects, and content.

 

### Governor Limits for Email Services 

 

Salesforce imposes limits on email sending: 

– A single transaction can have a maximum of *10 email invocations*. 

– A maximum of *10 Messaging.SendEmail()* method calls per transaction. 

 

## Inbound Email Messaging: 

 

Inbound Email Messaging allows Salesforce to receive emails and process their contents dynamically. It provides an interface called Messaging.InboundEmailHandler, which captures incoming emails and stores their content in associated objects along with attachments. 

 

### Key Features: 

– Receive emails from external systems. 

– Extract and store email content in Salesforce records. 

– Handle attachments dynamically. 

 

## Outbound Email Messaging: 

 

Outbound Email Messaging is used to send emails with customizable content. Salesforce allows sending emails in *PlainText* or *HTML* format using two primary classes: 

 

  1. *Messaging.SingleEmailMessage Class* – For sending emails to one or more recipients dynamically.
  2. *Messaging.MassEmailMessage Class* – For sending bulk emails using predefined templates.

 

### Messaging.SingleEmailMessage Class 

 

This class enables sending emails to multiple recipients dynamically at runtime, supporting attachments and various configurations. 

 

#### Syntax: 

Apex 

 

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

 

 

#### Key Methods: 

 

– setToAddresses(List<String>) – Defines recipients’ email addresses (Max: 100). 

– setCCAddresses(List<String>) – Adds CC recipients (Max: 25). 

– setBCCAddresses(List<String>) – Adds BCC recipients (Max: 25). 

– setReplyTo(String) – Sets the reply-to email address. 

– setSenderDisplayName(String) – Defines the sender’s display name. 

– setSubject(String) – Sets the email subject. 

– setPlainTextBody(String) – Defines the plain text email content. 

– setHTMLBody(String) – Defines the HTML email content. 

 

#### Sending Email Notification: 

 

Apex 

 

Messaging.SendEmail(new Messaging.SingleEmailMessage[]{emailObject}); 

 

 

#### Use Case: 

 

Write an Apex program to send an email notification to a candidate when a new candidate record is created. 

 

Class Code: 

public class CandidateHandler 

{ 

Public static void SendEmailToCandidates(List<Candidate c> lstCandidates) 

{ 

if(! lstCandidates.isEmpty()) 

 

{ 

List<Messaging.SingleEmailMessage> lstEmailsToCandidate = new List<Messaging.SingleEmailMessage>(); 

 

for(Candidate  c cnd : lstCandidates) 

{ 

// Prepare the Email Notification… 

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

 

string[] toEmailIDs = new string[]{cnd.Email_ID c, ‘kajalyadavky99@gmail.com’}; email.setToAddresses(toEmailIDs); 

/* 

string[] ccEmailIds = new string[]{cnd.Email_ID c, ‘ykajal0571@gmail.com’}; email.SetCCAddresses(ccEmailIDs); 

 

string[] bccEmailIDs = new string[]{cnd.Email_ID c, ykajal2022@gmail.com’}; email.SetBCCAddresses(bccEmailIDs); 

*/ 

 

email.SetSenderDisplayName(‘Candidate Record Creation Notification Alert’); email.SetReplyTo(‘ kajalyadavky99@gmail.com’); 

string emailSubject = ‘Congratulations ‘+ cnd.Name + ‘…!! Your Candidature has been successfully submitted for the position.’; 

email.SetSubject(emailSubject); 

 

string emailHTMLContent = ‘Dear ‘+ cnd.Name + ‘, <br/> <br/> ‘+ 

‘Thanks for showing interest in our organization positions. <br/><br/> ‘+ 

‘We are pleased to inform you, that your candidature has been successfully submitted for 

Leave a Comment

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