Email Services in Salesforce with simple example

What is an Email service in Salesforce?

Email services are automated processes that use Apex classes to process the contents, headers, and attachments of inbound email.

You can associate each email service with one or more Salesforce-generated email addresses to which users can send messages for processing.
The general template to create the apex class for the email services is:

How Email Services works in Salesforce

How Email Services works in Salesforce

1 global class myHandler implements Messaging.InboundEmailHandler {
2       global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
3           Messaging.InboundEmailResult result = newMessaging.InboundEmailresult();
4           return result;
5       }
6   }

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:

1 /**
2  * Email services are automated processes that use Apex classes
3  * to process the contents, headers, and attachments of inbound
4  * email.
5  */
6 global class CreateContactFrmEmail implements Messaging.InboundEmailHandler {
7  
8     global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
9     Messaging.InboundEnvelope envelope) {
10  
11         Messaging.InboundEmailResult result = newMessaging.InboundEmailresult();
12  
13         String subToCompare = 'Create Contact';
14  
15         if(email.subject.equalsIgnoreCase(subToCompare))
16         {
17             Contact c = new Contact();
18             c.LastName = email.plainTextBody;
19             insert c;
20  
21                 // Save attachments, if any
22             for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
23             Attachment attachment = new Attachment();
24  
25             attachment.Name = tAttachment.fileName;
26             attachment.Body = Blob.valueOf(tAttachment.body);
27             attachment.ParentId = c.Id;
28             insert attachment;
29             }
30  
31             //Save any Binary Attachment
32             for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
33             Attachment attachment = new Attachment();
34  
35             attachment.Name = bAttachment.fileName;
36             attachment.Body = bAttachment.body;
37             attachment.ParentId = c.Id;
38             insert attachment;
39             }
40         }
41  
42     result.success = true;
43         return result;
44     }
45  
46     static testMethod void testCreateContactFrmEmail() {
47         Messaging.InboundEmail email = new Messaging.InboundEmail() ;
48         Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();
49  
50         email.subject = 'Create Contact';
51         email.plainTextBody = 'FromEmail';
52         env.fromAddress = 'ilovenagpur@gmail.com';
53  
54         CreateContactFrmEmail creatC = new CreateContactFrmEmail();
55         creatC.handleInboundEmail(email, env );
56     }
57 }

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

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

Email Service Information - Salesforce

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

Email Address Information - Salesforce

Email Address Information - Salesforce

Note: The domain name in Email address must qualify the domain name entered at “email services” in first step. For example : we have entered “gmail.com” in first step that means it will accept the email address only from the gmail.com and that’s why in second step I have used email address displayed in screen shot.
After all the above steps, one email address is generated by the salesforce. Send the email to that address with subject line “Create Contact” and contact name in email body.
In the above tutorial, I have used very simple example just to demonstrate that how the email services works in the salesforce.

Governance limit of Email services in salesforce:
Salesforce limits the total number of messages that all email services combined, including On-Demand Email-to-Case, can process daily. Messages that exceed this limit are bounced, discarded, or queued for processing the next day, depending on how you configure the failure response settings for each email service. Salesforce calculates the limit by multiplying the number of user licenses by 1,000, up to a daily maximum of 1,000,000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值