Email Services in Salesforce with simple example

本文介绍Salesforce中邮件服务的工作原理及其应用场景。通过创建Apex类处理邮件内容、附件等,实现自动化流程,如从邮件创建联系人。此外,还讨论了邮件服务的配置步骤及每日消息处理限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

### 回答1: Salesforce Lightning 是一种用于构建功能丰富的销售、客户服务和市场营销应用程序的开发平台。它使用 JavaScript 作为客户端脚本语言,可以通过 REST API 与服务器端进行通信。 下面是一个使用 Python 通过 REST API 访问 Salesforce Lightning 的示例: ```python import requests # Replace <your_username> and <your_password> with your Salesforce login credentials auth = ('<your_username>', '<your_password>') # Replace <instance_url> with the URL of your Salesforce instance instance_url = '<instance_url>' # Set the API version to use api_version = 'v48.0' # Set the URL for the REST API resource you want to access resource_url = f'{instance_url}/services/data/{api_version}/sobjects/Account/describe' # Send the GET request and store the response response = requests.get(resource_url, auth=auth) # Print the response status code print(response.status_code) # Print the response body print(response.json()) ``` 在这个示例中,我们使用 Python 的 `requests` 库向 Salesforce 发送了一个 GET 请求,访问了 Salesforce 的 `Account` 对象的描述信息。如果请求成功,将会返回状态代码 200 和 `Account` 对象的描述信息。 注意,要使用这个示例,你需要在 Salesforce 中创建一个账户,并将你的登录凭据和 Salesforce 实例 URL 替换到示例中。此外,你还需要在你的 Salesforce 实例中启用 REST API。 希望这个示例能帮助你了解如何使用 Python 通过 REST API 与 Salesforce Lightning 通信。如果你有任何其他问题,请随时给我留言。 ### 回答2: Salesforce Lightning是Salesforce的一种用户界面框架,用于创建现代化、交互式的Salesforce应用程序。REST API是一种基于HTTP协议的Web服务,用于在不同的应用程序之间进行通信。 要使用Python与Salesforce Lightning结合使用REST API,可以按照以下步骤进行: 1. 首先,导入必要的Python库,如requests和json。安装这些库可以使用pip命令。 2. 获取Salesforce实例的访问令牌(access token)。可以使用OAuth 2.0进行身份验证,并使用自己的凭据来获取令牌。 3. 在Python中,我们可以使用requests库发送HTTP请求来与Salesforce REST API进行通信。可以使用GET、POST、PUT和DELETE等HTTP方法来执行不同的操作。 4. 可以使用URL来指定要访问的Salesforce对象和操作。例如,要获取所有账户记录,可以使用以下URL: https://<Salesforce实例URL>/services/data/vXX.X/query?q=SELECT Id, Name FROM Account 5. 使用requests库发送对应的HTTP请求,将访问令牌作为请求头部(headers)的一部分添加到请求中。 6. 解析从Salesforce REST API返回的响应,可以使用json库将响应中的JSON数据转换为Python对象。 7. 根据需求,可以进行进一步的操作,如创建、更新、删除记录。 以下是一个使用Python与Salesforce Lightning结合使用REST API的示例代码: ```python import requests import json # 获取访问令牌 access_token = "<YOUR_ACCESS_TOKEN>" # 要获取的Salesforce对象和操作的URL url = "https://<Salesforce_instance_url>/services/data/vXX.X/query?q=SELECT Id, Name FROM Account" # 添加访问令牌到请求头部 headers = {"Authorization": "Bearer " + access_token} # 发送GET请求 response = requests.get(url, headers=headers) # 解析JSON响应 data = json.loads(response.text) # 输出结果 for record in data["records"]: print(record["Id"], record["Name"]) ``` 以上就是一个简单的使用Python与Salesforce Lightning结合使用REST API的示例。通过类似的方式,可以执行其他操作,如创建、更新和删除记录等。 ### 回答3: Salesforce Lightning是Salesforce的一种应用界面模式,可以帮助用户更加便捷地操作Salesforce平台上的数据和功能。而REST API是一种用于与网络上的资源进行交互的技术,可以通过HTTP协议发送请求并获取响应数据。 如果要在Python中使用Salesforce Lightning的REST API进行开发,以下是一个示例: 首先,我们需要使用Python的请求库来发送HTTP请求。你可以使用Python的`requests`库,首先需要安装它,然后在你的代码中导入它。 ```python import requests ``` 然后,我们需要构建Salesforce REST API的请求URL,并添加所需的请求头信息。 ```python base_url = "https://your_salesforce_instance.salesforce.com" api_version = "vXX.X" endpoint = "/services/data/{api_version}/" token = "your_oauth_token" url = base_url + endpoint headers = {"Authorization": "Bearer " + token, "Content-Type": "application/json"} ``` 接下来,我们可以使用`requests`库发送各种类型的HTTP请求,例如GET、POST、PATCH、DELETE等。下面是一个使用Salesforce REST API获取所有账户的示例: ```python def get_all_accounts(): resource = "sobjects/Account" query_url = url + resource response = requests.get(query_url, headers=headers) return response.json() ``` 或者,我们可以创建新的账户,如下所示: ```python def create_account(account_data): resource = "sobjects/Account" create_url = url + resource response = requests.post(create_url, headers=headers, json=account_data) return response.json() ``` 这只是一个简单的示例,你可以根据实际需求进一步开发。希望这个示例能够帮助你理解如何使用Python与Salesforce Lightning的REST API进行交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值