What is Inbound Email Service?

Inbound Email Service: When we get an email from the external system to Salesforce, the apex class will process the emails, attachments & perform the requested operation. To perform this you have to write apex class by implementing the “Messaging.InboundEmailHandler” interface. And also there are some predefined classes under Messaging namespace to implement this. Below are the predefined classes.

  1. InboundEmail.
  2. InboundEmail.Header
  3. InboundEmail.BinaryAttachment
  4. InboundEmail.TextAttachment
  5. InboundEmailResult
  6. InboundEnvelope

See the below example program to handle the inbound email. Below example program creates a new account when salesforce org receives an email.

Inbound Email Service Apex Class

global class CreateAccountEmailServiceExample implements Messaging.InboundEmailHandler {
	global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,Messaging.InboundEnvelope envelop){
		Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
		String accname = email.fromName;
		String accdes = email.plainTextBody;
		String accIndus = email.Subject;
		Account ac = new Account(Name = accname, description = accdes, Industry = accIndus);
		insert ac;
		return null;
	}
}

What is Email Sevice?

Email Sevice is an automated process that use apex classes to process the contents, headers, attachments of Inbound email. We can associate each email service with one or more salesforce generated email addresses to which users can send messages for processing.

How to configure Email Service?

Below are the steps to create an email service.

  • Setup -> Develop -> Email Services Click on New Email Service button.
  • Fill the required details like Email Service Name, APexClass and check Active checkbox & click on SaveandNewEmailAddress. See the below image for reference.

Email Service

  • Again fill required details and click on save.

You will see one auto-generated email address. Copy this & send an email to this. Salesforce will receive that email and create a new account based on the logic of the above code.