email service salesforce

Email Services in Salesforce with Simple Example

What is an Email Administration in Salesforce?

Email administrations are computerized forms that utilization Apex classes to process the substance, headers, and connections of inbound email. You can relate each email administration with at least one Salesforce-created email delivers to which clients can send messages for preparing. The general layout to make the summit class for the email administrations is:

global class myHandler implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
          return result;
      }
  }

dont miss out iconDon't forget to check out: All About Salesforce Marketing Cloud, Email studio and Content Builder

Example of Email Service – Creating Contact from email

Presumption –

  • Subject should contain word “Create Contact”
  • Body contains only Contact Name.

Apex Code with test method:

global class CreateContactFrmEmail implements Messaging.InboundEmailHandler { 
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {
 
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
 
        String subToCompare = 'Create Contact';
 
        if(email.subject.equalsIgnoreCase(subToCompare))
        {
            Contact c = new Contact();
            c.LastName = email.plainTextBody;
            insert c;
 
                // Save attachments, if any
        	for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
          	Attachment attachment = new Attachment();
 
          	attachment.Name = tAttachment.fileName;
          	attachment.Body = Blob.valueOf(tAttachment.body);
          	attachment.ParentId = c.Id;
          	insert attachment;
        	}
 
        	//Save any Binary Attachment
        	for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
          	Attachment attachment = new Attachment();
 
          	attachment.Name = bAttachment.fileName;
          	attachment.Body = bAttachment.body;
          	attachment.ParentId = c.Id;
          	insert attachment;
        	}
        }
 
    result.success = true;
        return result;
    }
 
    static testMethod void testCreateContactFrmEmail() {
    	Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();
 
        email.subject = 'Create Contact';
        email.plainTextBody = 'FromEmail';
        env.fromAddress = '[email protected]';
 
        CreateContactFrmEmail creatC = new CreateContactFrmEmail();
        creatC.handleInboundEmail(email, env );
    }
}
 

After creating the above Apex class, click Your Name | Setup | Develop | Email Services.

  • Click New Email Service to define a new email service.
  • Select the above apex class, add the email address from where to accept the request, and activate the service.

After filling the form, click on “Save and New Email Address”

dont miss out iconCheck out another amazing blog by Sumit here: Effective Ways To Use Salesforce For Customer Retention

Note: The space name in the Email address must qualify the area name entered at "email services" in the initial step. For instance: we have entered "gmail.com" in the initial step that implies it will acknowledge the email address just from the gmail.com and that is the reason in the second step I have utilized the email address shown in the screenshot.

After all the above advances, one email address is created by the salesforce. Send the email to that address with the feature "Reach name in the email body.

In the above instructional exercise, I have used an essential model just to display how the email organizations work in the salesforce.

Administration breaking point of Email benefits in salesforce:

Salesforce limits the total number of messages that all email organizations joined, recalling For Demand Email-to-Case, can process step by step. Messages that outperform this limit are ricocheted, discarded, or lined for setting up the next day, dependent upon how you organize the mistake response settings for each email organization. Salesforce learns the limit by copying the amount of customer licenses by 1,000, as long as a step by step breaking point of 1,000,000.

Popular Salesforce Blogs