Email Services in Salesforce
Author
October 1, 2019
Email services in Salesforce are automated processes that use Apex class to process the Attachments, Headers, and Contents of an inbound email. We can associate each email service with one or more Salesforce-generated email addresses through which users can send messages for processing.
Use of Apex In Email Services
Apex class act as a medium to process incoming email messages. When we set up an email service in salesforce, we need to generate a special email address in which salesforce will receive emails.
We can use Apex to receive and process email and attachments from the external system to the Salesforce Platform.
The email that is sent by the external user is received by the Apex email service in salesforce and processed by Apex classes that use the Inbound Email object.
Apex email service creates an Inbound Email object that contains the contents and attachments of that email which has been sent by the external user.
We can use the Apex classes to implement the Messaging. Inbound Email Handler interface to handle the inbound email message.
How Email Services in Salesforce Works?
Email Services in Salesforce uses predefined Classes
- Inbound Email Handler
- Inbound Email Binary Attachments
- Inbound Email Inbound Envelope
- Inbound Email Result
Methods in Inbound Email Properties
We can access an Inbound Email object to get the headers, contents, and attachments of inbound email messages, and perform many functions.
- Binary Attachments
- CC Addresses
- From Address
- From Name
- Headers
- HTML Body
- HTML Body Is Truncated
- In Reply To
- Message-Id
- Plain Text Body
- Plain Text Body Is Truncated
- Reply To
- Subject
- Text Attachments
- To Addresses
Inbound Envelope Properties
From Address – The name that appears in From field of the envelope,
To Address – The name that appears in the field of the envelope.
Messaging Inbound Envelope
The object of this class keeps the information of the envelope (From address and to address) associated with inbound email.
Messaging Inbound Email Result
The Inbound Email Result is used to return the result of an email service. To access email services in Salesforce we will activate email service.
Example of Email Service – Change the Opportunity Stage to ‘Closed Won’.
- Subject of Email: USH Youth Programs Number – 0062S00000t7FCn has been Placed.
- Email Body: Order Number is #9871 Please Use it as a Reference.
Apex Class
@TestVisible
global class OrderNumber implements Messaging.InboundEmailHandler {
@TestVisible
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
String inputString= email.subject;
String reser= email.plainTextBody;
String sub=inputString.substringAfter(‘Number’);
String sub2=sub.substringBefore(‘has’);
sub2 = sub2.trim();
String r1=reser.substringAfter(‘is’);
String r2=r1.substringBefore(‘Please’);
r2=r2.trim();
r2=r2.replace(‘\n\n’,”);
Opportunity op = [Select id, StageName from Opportunity where id=:sub2];
Opportunity op1 = [Select id,Reservation_Number__c from Opportunity where id=:sub2];
// System.debug(r1);
//System.debug(op1);
op.StageName = ‘Closed Won’;
op1.Reservation_Number__c = r2;
update op;
update op1;
result.success = true;
System.debug(result);
return result;
}
}
Test Class
@isTest(SeeAllData=True)
private class OrderNumberTest {
static testMethod void validateHelloWorld() {
Test.startTest();
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
Opportunity op = new Opportunity (Id=’0062v00001CLZcIAAX’,StageName=’Closed Won’);
update op;
Opportunity op1 = new Opportunity (Id=’0062v00001CLZcIAAX’,Reservation_Number__c=’#9871′);
update op1;
email.subject= ‘USH Youth Programs Number – 0062S00000t7FCn has been Placed’;
email.plainTextBody= ‘ Order Number is #9871 Please Use it as a Reference ‘;
env.fromAddress=’xyz@mirketa.com’;
Test.stopTest();
OrderNumber ac = new OrderNumber();
ac.handleInboundEmail(email, env );
}
}
After creating Above Apex Class follow the steps mentioned below.
- Go to Email Services in Your Org.
- Click New to define a new email service.
- Select the above apex class and add the email address from where to accept the request.
- Activate the service.
- Next, click the “Save and New Email Address”.
- Enter the details that are required.
- Click the “Save” button.
- Salesforce will create an email.
- Copy that email and send the email to that email generated by Email Service.
Recent Posts
-
XML Parsing: Using MINIDOM Vs Element Tree (etree) in Python02 Jul 2025 Blog
-
A step by step Guide to create Salesforce web-to-lead form30 Jun 2025 Blog
-
How AI is Transforming User Experience Design in 202526 Jun 2025 Blog
-
How a Salesforce NPSP Consultant Can Elevate Nonprofit Impact25 Jun 2025 Blog
-
Salesforce Load and Performance Testing: Essentials, Importance & Execution23 Jun 2025 Blog
-
Salesforce Website Integration Boost Leads, Automation & Customer Experience11 Jun 2025 Blog
-
Driving Results in Manufacturing with Salesforce Manufacturing Cloud11 Jun 2025 Blog
-
Accelerating Growth with NetSuite SuiteCommerce02 Jun 2025 Blog
-
Salesforce Service Cloud Services streamlining operations29 May 2025 Blog
-
AI for Nonprofits: Mirketa & Exec Precision Webinar27 May 2025 Press Release
-
AI for Nonprofits: Use Cases, Tools & Implementation Strategies20 May 2025 Webinar
-
Javascript Frameworks for Salesforce Lightning Design System18 May 2025 Blog
-
Building a Smart Campus with Salesforce Student Information System: A Road to Smarter Education16 May 2025 Blog
-
Salesforce Nonprofit Cloud: Benefits & Consultant Role15 May 2025 Blog
-
Salesforce Consulting for Nonprofits: Maximize Impact09 May 2025 Blog
-
What to Expect from a Salesforce Admin Service Provider09 May 2025 Blog
-
Maximizing Efficiency with Salesforce Cloud Integration Services09 May 2025 Blog
-
Step-by-Step Guide to Salesforce NPSP Implementation09 May 2025 Blog
-
A Guide on How to Use Salesforce Agentforce for Manufacturing02 May 2025 E-Book
-
Choosing the Right Salesforce Integration Partner: A Complete Guide22 Apr 2025 Blog
-
Salesforce Higher Education: Transforming Modern Universities15 Apr 2025 Blog
-
AI Agents The Future of Business Applications09 Apr 2025 Blog
-
Why Purpose-Built AI Agents Are the Future of AI at Work07 Apr 2025 Blog
-
How the Atlas Reasoning Engine Powers Agentforce03 Apr 2025 Blog
-
Leveraging AI for Code Analysis, Real-Time Interaction, and AI-driven Documentation02 Apr 2025 Use-case