Apex电子邮件

您可以使用 Apex 处理入站和出站电子邮件。

将 Apex 与以下电子邮件功能结合使用:

  • 入站电子邮件
    使用 Apex 处理发送到 Salesforce 的电子邮件。
  • 出站电子邮件
    使用 Apex 处理从 Salesforce 发送的电子邮件。

入站电子邮件

使用 Apex 处理发送到 Salesforce 的电子邮件。

您可以使用 Apex 接收和处理电子邮件和附件。Apex 收到电子邮件 电子邮件服务,并由利用 InboundEmail 对象的 Apex 类进行处理。

注意

Apex 电子邮件服务仅在 Developer、Enterprise、Unlimited 和 Performance 中可用 版本组织。

请参阅 Apex 电子邮件服务。

出站电子邮件

使用 Apex 处理从 Salesforce 发送的电子邮件。您可以使用 Apex 发送个人电子邮件和群发电子邮件。电子邮件可以包含所有标准电子邮件 属性(例如主题行和密件抄送地址),使用 Salesforce 电子邮件模板, 并且是纯文本或 HTML 格式,或由 Visualforce 生成的格式。

注意

Visualforce 电子邮件 模板不能用于群发电子邮件。

您可以使用 Salesforce 以 HTML 格式跟踪电子邮件的状态,包括 电子邮件已发送、首次打开和上次打开,以及打开的总次数。要使用 Apex 发送个人和群发电子邮件,请使用以下类:SingleEmail消息实例化用于发送单个电子邮件的电子邮件对象。语法 是:

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

MassEmailMessage (群发电子邮件)实例化用于发送群发电子邮件的电子邮件对象。语法 是:

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

消息包括 static 方法,该方法将 使用 OR 类实例化的电子邮件对象,并返回一个 SendEmailResult 对象。sendEmailSingleEmailMessageMassEmailMessage

发送电子邮件的语法 是:

Messaging.sendEmail(new Messaging.Email[] { mail } , opt_allOrNone);

其中 是 或 。EmailMessaging.SingleEmailMessageMessaging.MassEmailMessage

可选参数 指定是否阻止传递所有 当任何消息因错误而失败时的其他消息 (),或者它是否允许传递没有错误的消息 ().缺省值为 。opt_allOrNonesendEmailtruefalsetrue包括 static 和 方法,它们可以 在发送任何电子邮件之前被调用,以确保发送组织不会 在提交交易和发送电子邮件时超出其每日电子邮件限制。这 语法 是:reserveMassEmailCapacityreserveSingleEmailCapacity

Messaging.reserveMassEmailCapacity(count);

Messaging.reserveSingleEmailCapacity(count);

其中表示总数 电子邮件将发送到的地址数。count请注意以下事项:

  • 在提交 Apex 事务之前,不会发送电子邮件。
  • 调用该方法的用户的电子邮件地址将插入到电子邮件标头的“发件人地址”字段中。都 返回、退回或收到的外出回复的电子邮件将发送给调用 方法。sendEmail
  • 每个事务最多 10 种方法。 使用 Limits 方法可以验证事务中的方法数。sendEmailsendEmail
  • 使用该方法发送的单封电子邮件计入发送组织的每日单封电子邮件限制。当此限制为 到达时,对方法 using 的调用将被拒绝,并且用户会收到错误代码。然而 允许通过应用程序发送单封电子邮件。sendEmailsendEmailSingleEmailMessageSINGLE_EMAIL_LIMIT_EXCEEDED
  • 使用该方法发送的大量电子邮件 计入发送组织的每日群发电子邮件限制。当达到此限制时, 对方法 using 的调用被拒绝,并且用户会收到错误代码。sendEmailsendEmailMassEmailMessageMASS_MAIL_LIMIT_EXCEEDED
  • SendEmailResult 对象中返回的任何错误都指示未发送电子邮件。

Messaging.SingleEmailMessage有一个名为 setOrgWideEmailAddressId 的方法。它接受对象的对象 ID。如果向 setOrgWideEmailAddressId 传递了有效的 ID,则会在电子邮件中使用该字段 标头,而不是登录用户的显示名称。发送电子邮件 标头中的 address 也设置为 中定义的字段。OrgWideEmailAddressOrgWideEmailAddress.DisplayNameOrgWideEmailAddress.Address

注意

如果同时定义了 setSenderDisplayName,则用户会收到错误。OrgWideEmailAddress.DisplayNameDUPLICATE_SENDER_DISPLAY_NAME

有关更多信息,请参阅 Salesforce 帮助中的组织范围的电子邮件地址 .

// First, reserve email capacity for the current Apex transaction to ensure
// that we won't exceed our daily email limits when sending email after
// the current transaction is committed.
Messaging.reserveSingleEmailCapacity(2);

// Processes and actions involved in the Apex transaction occur next,
// which conclude with sending a single email.

// Now create a new single email message object
// that will send out a single email to the addresses in the To, CC & BCC list.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

// Strings to hold the email addresses to which you are sending the email.
String[] toAddresses = new String[] {'user@acme.com'}; 
String[] ccAddresses = new String[] {'smith@gmail.com'};
  

// Assign the addresses for the To and CC lists to the mail object.
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);

// Specify the address used when the recipients reply to the email. 
mail.setReplyTo('support@acme.com');

// Specify the name used as the display name.
mail.setSenderDisplayName('Salesforce Support');

// Specify the subject line for your email address.
mail.setSubject('New Case Created : ' + case.Id);

// Set to True if you want to BCC yourself on the email.
mail.setBccSender(false);

// Optionally append the Salesforce email signature to the email.
// The email address of the user executing the Apex Code will be used.
mail.setUseSignature(false);

// Specify the text content of the email.
mail.setPlainTextBody('Your Case: ' + case.Id +' has been created.');

mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created.<p>'+
     'To view your case <a href=https://MyDomainName.my.salesforce.com/'+case.Id+'>click here.</a>');

// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

外部服务

外部服务将您的 Salesforce 组织连接到 Salesforce 外部的服务,例如 员工银行服务。注册外部服务后,可以在 您的 Apex 代码。在外部服务的已注册 API 中定义的对象和操作 specification 成为命名空间中的 Apex 类和方法。已注册服务的架构类型映射到 Apex 类型,并且 是强类型的,使 Apex 编译器为您完成繁重的工作。例如,您可以 从 Apex 对外部服务进行类型安全标注,而无需使用该类或对 JSON 字符串执行转换。

ExternalServiceHttp