Email Services in Salesforce with a simple example

Email Services in Salesforce with a simple example

Email Services is a feature of where, the user can assign an Apex Class that implements the Messaging.InboundEmailHandler interface to a configuration which allows you to process the email contents, headers and attachments. Using this Information, you can cater a variety of requirements . Listing few, create a new contact if one does not exists with that email address, receive job applications and attached the person’s resume to their record.

Email services are basically an option to a developer to develop a functionality , where an email can be received at a particular address and some operations can happen on the data retrieved from the email. These operations are done by the Apex Code, thus giving us a spectrum of options that fulfils requirements.

Explaining few,

A track of contacts can be kept , once the email is received the contacts are checked for the email address and if not a new contact is created and the attachments in the mail can be added to the new contact record.

In a Human resources management system, All job posting can have a email to it . All the job applications can be received via email and the subject can become the Job title the applicant is applying to. The email service automatically receives the email and creates a job application record using the subject of the email to categorise between job applied for and the resumes can be attached to the job application record as attachments.

Now to start using them,

You can Navigate to Salesforce Email Services using this path : Setup -> Develop -> Email Services,

OR just type Email services in the Quick Find Box.

Click the “New Email Service” button to get started and fill out the form.

The Form has a number of Options, Including Name, Apex Class Etc. Some Options are very handy, “Enable Error Routing” will send the inbound email to an alternative email address when the processing fails. You can can also specify email address to accept mail from.

Note : Make Sure you create a Basic Class that implements the Messaging.InboundEmailHandler interface that can be assign as the class here.

After you save the new email service, you will need to scroll down to the bottom of the page and create a new email address for the service. An email service can have multiple email addresses and therefore process the same message differently for each address. When you create a new email service address you specify the “Context User” and “Accept Email From”. The email service uses the permissions of the Context User when processing the inbound message. So you could, for example, have the same email service that accepts email from US accounts and processes them with a US context user. After the form is submitted the Platform will create a Unique Email Address, To which the email has to be sent. For Example, .

Now that we are done with configuring the Email Services, We can move to write the Apex Class as per Requirements.

Scenario :  Create a Contact using the Information in the email and add attachments.

[sourcecode language=”java”]
global class EmailService implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
list<Attachment>att = new list<Attachment>();
Contact con = new contact();
con.FirstName = email.fromname.substring(0, email.fromname.indexOf(‘ ‘));
con.LastName = email.fromname.substring(email.fromname.indexOf(‘ ‘));
} = envelope.fromaddress;
con.description = email.plaintextbody;
con.title = email.subject;
insert con;

if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
for (integer i = 0;i<email.binaryAttachments.size();i++) {
Attachment attachment = new Attachment();
attachment.ParentId = con.Id;
attachment.Name = email.binaryAttachments[i].filename;
attachment.Body = email.binaryAttachments[i].body;
insert att;

if (email.textAttachments != null && email.textAttachments.size() > 0) {
for (integer i = 0; i < email.textAttachments.size(); i++) {
Attachment attachment = new Attachment();
attachment.ParentId = con.Id;
attachment.Name = email.textAttachments[i].filename;
attachment.Body = blob.valueOf(email.textAttachments[i].body);
insert att;
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
return result;

Similarly you can have any sort of code in
the apex class, Depends solely on what the 
requirement is. 
Basically the lines of code can use the 
information available and perform some 
operations based on the requirements. 
Happy coding!!!

Leave a Comment

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

Recent Posts

salesforce for financial services transforming customer engagement operational effectiveness
Salesforce for Financial Services: Transforming Customer Engagement & Operational Effectiveness
einstein copilot transform your salesforce experience
How Einstein Copilot Actions Can Transform Your Salesforce Experience
how can salesforce experience cloud drive customer engagement rates
How Can Salesforce Experience Cloud Drive Customer Engagement Rates?
salesforce world tour essentials mumbai
Salesforce World Tour Essentials Mumbai (19 June, 2024)
benefits of using salesforce for higher education Industry
Benefits Of Using Salesforce For Higher Education Industry?
Scroll to Top