Inbound Email Handler in Salesforce | Techila
single,single-post,postid-1204,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive

Inbound Email Handler in Salesforce


Inbound Email Handler in Salesforce



 We all write Workflows or triggers to send email from Salesforce but did you know that you can send emails back to Salesforce org and perform task based on the content of the email 

Inbound Email Service :

Email Service is an apex class which implements Messaging.InboundEmailhandler interface.  This interface enables you to read the Subject,Content,headers and attachment of the incoming email. You read the email in the Email service class and perform DML operations such as creating new records or updated the existing ones as per your requirement. 

To create Email services click Setup–> Develop–> Email Services and then create a apex class using the sample code mentioned on the email services landing page. See the snapshot below : 


Once the class is create go ahead and click on New Email Service button enter the information as displayed in the image below, Change the Error routing address from to your email address so that you receive error notifications when the email processing fails. 



Click on “Save and New email Address” , Once done choose the context user and keep the Accept email from field blank to accept emails from all kind of domains. Clicking on Save will generate a email address automatically ,this email address will look somewhat like

 Now we can write the logic which is to be executes when an email is sent to the above created email address. Copy the below code and replace it in the email service class created initially in the post. (myHandler )

global class myHandler  implements Messaging.InboundEmailHandler {

  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,

    Messaging.InboundEnvelope envelope) {

    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

    Contact con = new Contact();

    con.FirstName = email.fromname.substring(0,email.fromname.indexOf(‘ ‘));

    con.LastName = email.fromname.substring(email.fromname.indexOf(‘ ‘));

    con.Email = envelope.fromAddress;

    insert con;

    return result;





Author: techila

No Comments

Post A Comment