Email Services or "email to Apex" is now generally available for all customers who have Apex Code enabled. Email Services lets you define email addresses, which can process Apex code. Send emails to Salesforce and process them with Apex code to create and update data from inbound emails. This sample code shows you how an email can be used to create new tasks for a contact record with a matching email address.

Example: Send an email to Saleforce with a follow-up note, will setup a task with a reminder 1 day for now.

What is new in the Spring’08 release

  • Must implement the Messaging interface
  • Implemented a namespace “Messaging”
  • Additional input parameter “Messaging. InboundEnvelope”
  • New and improved security model
  • Uses Email Authentication protocols (SPF, SenderId, DomainKeys) to secure the email addresses

Note: For people who were using the previous Developer Preview version.

Your code will not work in the Spring '08 release, until you change it to the new format, for more detailed information on the changes, please see the Spring '08 release notes.

Email Screen Shot2.jpg

A new activity is created and shown in the related list of the contact with the matching email address.

Email activity Shot.jpg

The task is created and the information in the email is used to populate the new task and the reminder is set for tomorrow.

Task Created Shot2.jpg

With the new Email Services in the Spring '08 release, we have made significant improvements to the security model and how to setup and manage Email Services and email addresses.

From the screen shot below, you can see the new setup screen, which lets you define your security settings and how you want to handle failure responses.

Email Services Setup Screen.jpg

Sample code for creating a new task

global class tasks implements Messaging.InboundEmailHandler {

global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
                                                  Messaging.InboundEnvelope env){

// Create an inboundEmailResult object for returning 
// the result of the Email Service
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

String myPlainText = '';

// Add the email plain text into the local variable

      myPlainText = email.plainTextBody.substring(0, email.plainTextBody.indexOf('<stop>'));
catch (System.StringException e)
     myPlainText = email.plainTextBody;
     System.debug('No <stop> in email: ' + e);

// new Task object to be created

Task[] newTask = new Task[0];

    // Try to lookup any contacts based on the email from address
    // If there is more than 1 contact with the same email address
    // an exception will be thrown and the catch statement will be called
try {
       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 and it will be created and appended to the contact record
     insert newTask;

System.debug('New Task Object: ' + newTask );
   // If there is an exception with the query looking up
   // the contact this QueryException will be called.
   // and the exception will be written to the Apex Debug logs

   catch (System.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 Email Service
  return result;

static testMethod void testTasks() {

// Create a new email and envelope object
   Messaging.InboundEmail email = new Messaging.InboundEmail();
   Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

// Create the plainTextBody and fromAddres for the test
	email.plainTextBody = 'Here is my plainText body of the email';
	email.fromAddress ='';

Tasks taskObj = new Tasks();
taskObj.handleInboundEmail(email, env);