Salesforce Inbound Email Service

by Rijwan Mohmmed
0 comment
Salesforce-Inbound-Email-Service-techdicer

Hello friends, today we will discuss Salesforce Inbound Email Service. It is a special process in which by apex class we can process incoming emails with/without attachments.
First, we need to set up email service. so it will generate an email address so we can receive all emails in salesforce whenever we get email on mail.

We create an email service so it will invoke a handleInboundEmail method on the Apex class. whenever an email is receiver on mail Id.

Salesforce inbound email serivces

Step -1: First Create an Apex class with Messaging.InboundEmailHandler implements – we create a task on the email received. and assign this task to contact which is retrieved by email.

global class EmailServiceTechdicer 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 emailResult = new Messaging.InboundEmailResult();
  try{
	   List<Contact> contList= [SELECT Id, Email FROM Contact WHERE Email = :email.fromAddress LIMIT 1];
	   
	   EmailMessage emailMessage = new EmailMessage();  
	   emailMessage.Status = '0';
	   emailMessage.FromAddress = email.FromAddress;
	   
	   if(email.toAddresses.size() > 0){
		   emailMessage.ToAddress = String.join( email.toAddresses, ';' );
	   }
	   
	   if(email.ccAddresses != null && email.ccAddresses.size() > 0){
		   emailMessage.CcAddress = String.join( email.ccAddresses, ';' );
	   }
	   emailMessage.FromName = email.FromName; // from name  
	   emailMessage.Subject = email.Subject; // email subject  
	   emailMessage.TextBody = email.plainTextBody; // email body  
	   emailMessage.RelatedToId = contList[0].Id;
	   insert emailMessage;

	   if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
		  for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
			Attachment attachment = new Attachment();
			// attach to the newly created contact record
			attachment.ParentId = contList[0].Id;
			attachment.Name = email.binaryAttachments[i].filename;
			attachment.Body = email.binaryAttachments[i].body;
			insert attachment;
		  }
       }	
       
  }
  catch (Exception e){
     System.debug('Error : ' + e);
  }
  emailResult.success = true;
  return emailResult;
 }
}

Step 2: Now add create a record for email service Setup > Email Services click on email services and create record

Fill the form and select our email inbound class and click save

salesforce-inbound-email-service-techdicer12
salesforce-inbound-email-service-techdicer12

Now click New email address which is on email service record details page related list

salesforce-inbound-email-service-techdicer13
salesforce-inbound-email-service-techdicer15
salesforce-inbound-email-service-techdicer15

Then click on SAVE button.

salesforce-inbound-email-service-techdicer16

STEP 3:- Testing of email service

1) Create one contact with the sender’s email address.
2) Then send email from given email domain to same email address.

salesforce-inbound-email-service-techdicer16

Send this email and check the latest contact, a new contact is inserted with an email Id and also task record created.

Check this: Automatically forward Gmail messages to Salesforce Org, In this way you don’t need to give your salesforce email service Id for sending an email. whenever you got email in Gmail it automatically forwards to the salesforce org email address.

What’s your Reaction?
+1
1
+1
0
+1
0
+1
0
+1
1
+1
0

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.