Capture Replies In Salesforce Using Email Services

Capture Replies In Salesforce Using Email Services

Sharing is caring!

Salesforce email service is an automated process that utilizes Apex classes to handle attachments, headers, and content of incoming emails. We can also combine any email service with one or more Salesforce-generated email addresses to send messages to get started with processing.

Apex class serves as a core part to handle incoming email messages.  When we set up an email service in Salesforce, we need to create a personalized email address to receive emails from Salesforce.

Types of Email Services in Salesforce

Email messages are known as a more stable and robust message exchange scheme in Salesforce. Email service in Salesforce is used when a user wants to send or receive an email from an external system. Let us now tell you about the most common types of email service in Salesforce.

Inbound email service in Salesforce and Outbound email service in Salesforce are the different types of Salesforce email services.

Inbound Email Services in Salesforce

With Inbound email services, you can receive and process emails and attachments from the external system to Salesforce using Apex. In this process, Email is received and accessed by the Apex email service and then further processed by the Apex class that uses an object for incoming email. The Apex Salesforce Email Service develops a login object, including the content and attachments of the email. Additionally, you can use the Apex class to implement the Salesforce Messaging interface, an incoming email handler, to process incoming Salesforce email messages.

Outbound Email Service in Salesforce

Outbound email service is used to send an email containing the Apex code to an external system. Salesforce offers two types of outbound email services. These are single email messaging and mass email messaging. When it comes to single email messaging,  email services are used to send one email, and group email services are used to send multiple emails at once.

Syntax For Single Email Messaging In Salesforce

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

Syntax For Mass Email Messaging In Salesforce

Messaging.MassEmailMessage email = new Messaging.MassEmailMessage();

Why Use Apex In Email Services?

1. With Apex, we can receive and process emails and attachments from systems external to the Salesforce platform.

2.  Using Inbound Email objects, Apex email service from Salesforce receives the emails sent by external users.

3. Inbound Email object with content and attachments of emails sent by the users is created by Apex email service.

4. To implement the Messaging. Inbound Email Handler interface to process the inbound email message, the Apex classes are used.

Steps To Create An Email Service

1. Click on Setup.

2. Type Email Service on the search box.

3. Click on the new Email service.

4. Choose the Apex classes that are mentioned below.

Code: Apex Classes- sendemailReply 

global class sendemailReply implements messaging.Inboundemailhandler {

   List ConList ;

    global messaging.InboundEmailresult handleInboundEmail(messaging.InboundEmail email, messaging.InboundEnvelope envelope) {

        ConList = new list ();

        messaging.InboundEmailresult result = new messaging.InboundEmailresult();

        string Activityname = email.fromname;

        String fromEmailAddresss = email.fromAddress;

        List ConList = [select id from Contact where email = : fromEmailAddresss limit 1];

        system.debug(List Of Contats :::::’+Conlist);

        string replyText = email.plaintextbody;

        string tasksubject = email.subject;

        Task tkobj = new Task();

        tkobj.Description = replyText;

        tkobj.Priority = ‘Medium’;

        tkobj.ActivityDate = System.today();

        tkobj.ReminderDateTime = System.now();

        tkobj.WhoId = Conlist[0].id;

        insert tkobj;

        return null;

    }

}

5) Click on the Active check box.

6) Click on Save And new Email Addresses.

7) Fill in all the required fields.

8)  Click on Save. 

9) Now you will Send Email

While you’re sending Email by using trigger, you need to implement these codes in your trigger – And setReplyTo is New Address  which is generated by email service.

 messaging.SingleEmailMessage mail=new messaging.SingleEmailMessage();

        string[] toaddresses = new string[]{address};

            string[] toCcaddresses = new string[]{cc};    

                mail.setToAddresses(toaddresses);     

        mail.setSubject(subject);

        mail.setPlainTextBody(body);

        mail.setCcAddresses(toCcaddresses);

    mail.setReplyTo(‘newreply@vb8g2h288gni2hx4mv06gee429mo5yx8otnpcrsg9kn2bmqrb.2w-ccrckea3.ap16.apex.salesforce.com’);

        messaging.sendEmail(new messaging.SingleEmailMessage[]{mail});

Governance Limit Of Email Services In Salesforce

Salesforce limits the no. of messages that can be processed by email services such as on-demand email each day. Depending on the error response configuration settings for every email service, messages are discarded or queued for processing later when they exceed the limit. Salesforce checks the limit by multiplying the number of user licenses by 1,000 to a maximum of 1,000,000 per day.

Salesforce contains all the standard services to process incoming and outgoing emails efficiently. However, to process the inbound ones, you have to create a handler inside the Apex class and then attach it to an email service and an email address.

In today’s ever-changing digital environment, your business needs are constantly evolving. Do you want to know how you can ensure your company grows alongside them and make the most of Salesforce investment? With many years of rich experience working across various domains and industries, Cloud Analogy, a leading Salesforce consulting partner, has perfected the art and science of delivering end-to-end and robust Salesforce consulting services to clients across the globe. Get in touch with Cloud Analogy certified Salesforce experts now!

Suraj

Suraj Tripathi

Salesforce Consultant | Solutions Engineering Head
"Suraj Tripathi, a certified Salesforce Principal Consultant of repute, is a wonderful mentor and leader. A certified Salesforce Architect and a 7x Salesforce Certified Platform Application Developer by passion and profession, Suraj has rich experience in languages such as Aura, HTML, Angular, Bootstrap, APEX, and JavaScript. With more than five years of expertise in Salesforce Development, Suraj has worked on more than 50+ projects out of which 20+ projects were related to Salesforce Integration, Writing Triggers, Batch classes, VisualForce pages, and Aura Components.

Hire the best Salesforce Development Company. Choose certified Salesforce Developers from Cloud Analogy now.

× How can I help you?