注意:
- 此教程需要访问 Oracle Cloud。要注册免费账户,请参阅开始使用 Oracle Cloud Infrastructure Free Tier 。
- 它使用 Oracle Cloud Infrastructure 身份证明、租户和区间示例值。完成实验室时,请将这些值替换为特定于您的云环境的值。
使用 Oracle Cloud Infrastructure Email Delivery Service HTTP 接口发送电子邮件
简介
Oracle Cloud Infrastructure (OCI) Email Delivery 是一项电子邮件发送服务,可为发送大量批量电子邮件和事务电子邮件提供快速可靠的托管解决方案。以前,OCI 电子邮件传送仅包含一个简单邮件传输协议 (Simple Mail Transfer Protocol,SMTP) 接口。但是,我们最近宣布推出新的 HTTPS REST API 来提交电子邮件,从而提高性能、消除配置复杂性并提高成功收件箱投放率。
目标
- 使用 OCI 电子邮件传送服务 HTTP 界面发送电子邮件。
先决条件
-
具有使用 OCI 电子邮件传送发送电子邮件的足够权限的用户。有关更多信息,请参见此处的策略。
-
OCI 电子邮件传送域。有关设置 OCI 电子邮件传送域的更多信息,请参阅使用 OCI 电子邮件传送发送电子邮件的分步说明。
任务 1:了解验证方法
HTTPS 电子邮件提交支持标准 OCI SDK 验证方法。在本教程中,我们将演示基于 API 密钥的验证和实例主体验证。有关详细信息,请参阅 OCI SDK 验证方法。
-
基于密钥的 API 验证
在此验证中,您将创建一个配置文件并将其存储在本地计算机上。因此,它只能从安全网络使用,当您方便地在本地存储私钥时。该文件包含以下详细信息。
- 用户 Oracle Cloud 标识符 (OCID)
- 租户 OCID
- 区域
- 私有密钥路径
- 指纹
有关如何创建和收集上述参数的更多信息,请参见 Required Keys and OCIDs 。
-
实例主用户验证
实例主用户验证涉及授权实例通过动态组在 OCI 服务上进行 API 调用。创建动态组、匹配规则和正确的策略。有关更多信息,请参见 Managing Dynamic Groups 。
要使实例能够发送电子邮件,请使用以下策略。
Allow dynamic-group <dynamic-group-name> to use email-family in compartment <compartment-name>
有关如何进一步定制这些权限的更多信息,请参阅电子邮件传送服务的详细信息。
任务 2:发送电子邮件
我们将使用 Python 软件开发工具包 (SDK),但可以替换任何 SDK 或 Oracle Cloud Infrastructure 命令行界面 (OCI CLI)。有关如何安装的更多信息,请参见 Software Development Kits and Command Line Interface 。
注:有关可用参数的完整列表,请参阅电子邮件传送 API 。
-
基于密钥的 API 验证
import oci #Create a default config config = oci.config.from_file() #Initialize service client with config file email_client = oci.email_data_plane.EmailDPClient(config) #Create the email details email_details = oci.email_data_plane.models.SubmitEmailDetails( sender = oci.email_data_plane.models.Sender( sender_address = oci.email_data_plane.models.EmailAddress( email = '<approved sender email>', name = '<sender name>'), compartment_id = '<compartment ocid>'), recipients = oci.email_data_plane.models.Recipients( to = [ oci.email_data_plane.models.EmailAddress( email = '<to email address>', name = '<to name>' )], cc = [ oci.email_data_plane.models.EmailAddress( email = '<cc email address>', name '<cc name>' )], bcc = [ oci.email_data_plane.models.EmailAddress( email = '<bcc email address>', name '<bcc name>' )]), subject = '<email subject>', body_text = '<email body>', ) #Submit the email email_client.submit_email(email_details)
从以下位置下载 Python SDK 客户端配置示例:configuration_example.py 。
-
实例主用户验证
import oci compartment_id = '<compartment ocid>' # By default this will hit the auth service in the region returned by http://169.254.169.254/opc/v2/instance/region on the instance. signer = oci.auth.signers.InstancePrincipalsSecurityTokenSigner() # In the base case, configuration does not need to be provided as the region and tenancy are obtained from the InstancePrincipalsSecurityTokenSigner identity_client = oci.identity.IdentityClient(config={}, signer=signer) #Create the email details email_details = oci.email_data_plane.models.SubmitEmailDetails( sender = oci.email_data_plane.models.Sender( sender_address = oci.email_data_plane.models.EmailAddress( email = '<approved sender email>', name = '<sender name>'), compartment_id = '<compartment ocid>'), recipients = oci.email_data_plane.models.Recipients( to = [ oci.email_data_plane.models.EmailAddress( email = '<to email address>', name = '<to name>' )], cc = [ oci.email_data_plane.models.EmailAddress( email = '<cc email address>', name '<cc name>' )], bcc = [ oci.email_data_plane.models.EmailAddress( email = '<bcc email address>', name '<bcc name>' )]), subject = '<email subject>', body_text = '<email body>', ) #Submit the email email_client.submit_email(email_details)
从以下位置下载 Python SDK 实例主体示例:instance_principals_examples.py 。
任务 3:批量测试
要保持发件人信誉和我们的信誉,请使用以下最佳实践进行批量测试。
-
在
discard.oracle.com
域中使用收据地址,例如example@discard.oracle.com
。OCI 电子邮件传送服务接受邮件,但不将其传送到收件箱。 -
如果将大量电子邮件发送到有效的电子邮件地址,则接收者会拒绝它们,从而导致许多硬退回。这对 IP 声誉产生了负面影响。要测试退回处理,请向不存在的域发送少量电子邮件。
相关链接
确认
- 作者 - Cody Brinkman(云架构师)
更多学习资源
浏览 docs.oracle.com/learn 上的其他实验室,或者通过 Oracle Learning YouTube 频道访问更多免费学习内容。此外,请访问 education.oracle.com/learning-explorer 以成为 Oracle Learning Explorer。
有关产品文档,请访问 Oracle 帮助中心。
Send Emails using the Oracle Cloud Infrastructure Email Delivery Service HTTP Interface
F96706-02
May 2024