Email Services in Salesforce (Email to Object)

Email services are automated processes that use Apex classes to process the contents, headers, and attachments of inbound emails.

Email services are automated processes that use Apex classes to process the contents, headers, and attachments of inbound emails. For example, you can create an email service that automatically creates contact records based on contact information in messages. Each email service can have one or more email service addresses that can receive messages for processing.

Step 1
Before creating an email service, create an Apex class that implements the Messaging.InboundEmailHandler interface.
Here is a sample code to create a Task for a contact object whose email address matches the sender’s email address.


global class CreateTaskEmailExample implements Messaging.InboundEmailHandler 
  global Messaging.InboundEmailResult  handleInboundEmail(Messaging.inboundEmail email, 
                                                       Messaging.InboundEnvelope env)
    // Create an InboundEmailResult object for returning the result of the 
    // Apex Email Service
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    String myPlainText= '';
    // Store the email plain text into the local variable 
    myPlainText = email.plainTextBody;
    // Create a new Task object 
    Task[] newTask = new Task[0];
    // Try to look up any contacts based on the from email address.
    // If there are more than one contacts with the same email address,
    // an exception will be thrown and the catch statement block will be executed.
      Contact vCon = [SELECT Id, Name, Email FROM Contact WHERE Email = :email.fromAddress   LIMIT 1];
      // Add a new Task to the contact record we just found above.
      newTask.add(new Task(Description =  myPlainText,
           Priority = 'Normal',
           Status = 'Inbound Email',
           Subject = email.subject,
           IsReminderSet = true,
           ReminderDateTime =,
           WhoId =  vCon.Id));
     // Insert the new Task 
     insert newTask;    
     System.debug('New Task Object: ' +;   
    // If an exception occurs when the query accesses 
    // the contact record, a QueryException is thrown.
    // The exception is written to the Apex debug log.
   catch (QueryException e) {
       System.debug('Query Issue: ' + e);
   // Set the result to true. No need to send an email back to the user 
   // with an error message
   result.success = true;
   // Return the result for the Apex Email Service
   return result;

Step 2
Create an Email-Service
To use email services, from Setup, click Develop | Email Services.

1. Click New Email Service to define a new email service.
2. Select the class that we created in the Step1.
3. Check the Active check box.
4. Optionally, configure this email service only to accept messages from certain senders, by listing their email addresses and domains in the Accept Email From text box. Leave this field blank if you want the email service to receive email from any email address.
5. Configure the Failure Response Settings.
6. Click Save to save the email service.

After you saved 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 can 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”.


After you submit the form, the platform will create a unique email address like the following.This is the address you send your email to for processing.

Step 3
Now to test the Email Service, send an email from your email ID to the above email address auto generated by the Email Service.


Step 4
Now, you can see a new Task is created for the contact that has email address as sender’s email address.


Governance limit of Email services in salesforce
Salesforce limits the total number of messages that all email services combined, including On-Demand Email-to-Case, can process daily. Messages that exceed this limit are bounced, discarded, or queued for processing the next day, depending on how you configure the failure response settings for each email service. Salesforce calculates the limit by multiplying the number of user licenses by 1,000, up to a daily maximum of 1,000,000.

Summary has all the necessary services to easily handle incoming and outgoing emails. To process the incoming emails, you simply create an inbound email handler, bind it to an email service and an email address, and you are good to go!

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.