Email Services in Salesforce

Email services in Salesforce are automated processes that uses Apex class to process the Attachments, Headers and Contents of an inbound email.

We can associate each email service with one or more Salesforce-generated email addresses through which users can send messages for processing.

Use of Apex In Email Services

Apex class act as a medium to process incoming email messages. When we set up an email service in salesforce, we need to generate a special email address in which salesforce will receive emails.

We can use Apex to receive and process email and attachments from the external system to the Salesforce Platform.

The email that is sent by the external user is received by the Apex email service in salesforce and processed by Apex classes that uses the Inbound Email object.

Apex email service creates an Inbound Email object that contains the contents and attachments of that email which has been sent by the external user.

We can use the Apex classes to implement the Messaging. Inbound Email Handler interface to handle the inbound email message.

How Email Services in Salesforce Works?

email services in salesforce

Email Services in Salesforce uses predefined Classes

  • Inbound Email Handler
  • Inbound Email Binary Attachments
  • Inbound Email Inbound Envelope
  • Inbound Email Result

Methods in Inbound Email Properties

We can access an Inbound Email object to get the headers, contents and attachments of inbound email message, and perform many functions.

  • Binary Attachments
  • CC Addresses
  • From Address
  • From Name
  • Headers
  • HTML Body
  • HTML Body Is Truncated
  • In Reply To
  • Message-Id
  • Plain Text Body
  • Plain Text Body Is Truncated
  • Reply To
  • Subject
  • Text Attachments
  • To Addresses

Inbound Envelope Properties

From Address – The name that appears in From field of the envelope,
To Address – The name that appears into the field of the envelope.

Messaging Inbound Envelope

The object of this class keeps the information of envelope (From address and to address) associated with inbound email.

Messaging Inbound Email Result

The Inbound Email Result is used to return the result of an email service. To access email services in Salesforce we will activate email service.

Example of Email Service – Change the Opportunity Stage to ‘Closed Won’.

  • Subject of Email: USH Youth Programs Number – 0062S00000t7FCn has been Placed.
  • Email Body: Order Number is #9871 Please Use it as a Reference.
  • Apex Class

    @TestVisible
    global class OrderNumber implements Messaging.InboundEmailHandler {
    @TestVisible
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
    String inputString= email.subject;
    String reser= email.plainTextBody;
    String sub=inputString.substringAfter(‘Number’);
    String sub2=sub.substringBefore(‘has’);
    sub2 = sub2.trim();
    String r1=reser.substringAfter(‘is’);
    String r2=r1.substringBefore(‘Please’);
    r2=r2.trim();
    r2=r2.replace(‘\n\n’,”);
    Opportunity op = [Select id, StageName from Opportunity where id=:sub2];
    Opportunity op1 = [Select id,Reservation_Number__c from Opportunity where id=:sub2];
    // System.debug(r1);
    //System.debug(op1);
    op.StageName = ‘Closed Won’;
    op1.Reservation_Number__c = r2;
    update op;
    update op1;
    result.success = true;
    System.debug(result);
    return result;
    }
    }

    Test Class

    @isTest(SeeAllData=True)
    private class OrderNumberTest {
    static testMethod void validateHelloWorld() {
    Test.startTest();
    Messaging.InboundEmail email = new Messaging.InboundEmail();
    Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
    Opportunity op = new Opportunity (Id=’0062v00001CLZcIAAX’,StageName=’Closed Won’);
    update op;
    Opportunity op1 = new Opportunity (Id=’0062v00001CLZcIAAX’,Reservation_Number__c=’#9871′);
    update op1;
    email.subject= ‘USH Youth Programs Number – 0062S00000t7FCn has been Placed’;
    email.plainTextBody= ‘ Order Number is #9871 Please Use it as a Reference ‘;
    env.fromAddress=’xyz@mirketa.com’;
    Test.stopTest();

    OrderNumber ac = new OrderNumber();
    ac.handleInboundEmail(email, env );

    }
    }

    After creating Above Apex Class follow the steps mentioned below.

    1. Go to Email Services in Your Org.
    2. Click New to define a new email service.
    3. Select the above apex class and add the email address from where to accept the request.
    4. Activate the service.
    5. Next, click the “Save and New Email Address”.
    6. Enter the details that are required.
    7. Click the “Save” button.
    8. Salesforce will create an email.
    9. Copy that email and send the email to that email generated by Email Service.
    Posted in email services address salesforce, email services in salesforce, how to create email services in salesforce, Mirketa, MirketaInc, Salesforce, salesforce consultant, Salesforce.com, what is email services in salesforce.

    2 Responses

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *