使用 Amazon SES API 发送原始电子邮件
关于电子邮件标头字段
简单邮件传输协议 (SMTP) 通过定义邮件信封及其部分参数来指定电子邮件将如何发送,但它本身与邮件内容无关。相反,Internet 邮件格式 (RFC 5322) 定义了如何创建邮件。

根据 Internet 邮件格式规范,每封电子邮件都包含标题和正文。标题包含邮件元数据,正文包含邮件本身。有关电子邮件标题和正文的更多信息,请参阅电子邮件格式和 Amazon SES。
使用 MIME
SMTP 协议最初设计用于发送仅包含 7 位 ASCII 字符的电子邮件。此规范使得 SMTP 不足以应对非 ASCII 文本编码(如 Unicode)、二进制内容或附件。多用途 Internet 邮件扩展标准 (MIME) 是为了能够使用 SMTP 发送许多其他种类的内容而制定的。

MIME 标准的工作方式是将邮件正文拆分为多个段,然后指定要对每个段执行的操作。例如,电子邮件正文的一个段可能是纯文本,另一个段可能是 HTML。此外,MIME 还允许电子邮件包含一个或多个附件。邮件收件人可以在电子邮件客户端内查看附件,也可以保存附件。

邮件标题和内容由一个空白行分隔。电子邮件的每个段都由一个边界 (表示每个段的开头和末尾的一个字符串) 分隔。

以下示例中的多段邮件包含一个文本和一个 HTML 段。它还包含一个附件。

From: "Sender Name" <sender@example.com>
To: recipient@example.com
Subject: Customer service contact info
Content-Type: multipart/mixed;boundary="a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a"--a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a
Content-Type: multipart/alternative;boundary="sub_a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a"--sub_a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printablePlease see the attached file for a list of customers to contact.--sub_a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable<html>
<head></head>
<body>
<h1>Hello!</h1>
<p>Please see the attached file for a list of customers to contact.</p>
</body>
</html>--sub_a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a----a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a
Content-Type: text/plain; name="customers.txt"
Content-Description: customers.txt
Content-Disposition: attachment;filename="customers.txt";creation-date="Sat, 05 Aug 2017 19:35:36 GMT";
Content-Transfer-Encoding: base64SUQsRmlyc3ROYW1lLExhc3ROYW1lLENvdW50cnkKMzQ4LEpvaG4sU3RpbGVzLENhbmFkYQo5MjM4
OSxKaWUsTGl1LENoaW5hCjczNCxTaGlybGV5LFJvZHJpZ3VleixVbml0ZWQgU3RhdGVzCjI4OTMs
QW5heWEsSXllbmdhcixJbmRpYQ==--a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a--

邮件的内容类型为 multipart/mixed,这表示邮件有很多个段 (在本例中为一个正文和一个附件),且接收客户端必须单独处理每个段。还有一个嵌套在正文部分中的段,该段使用 multipart/alternative 内容类型。此内容类型表示每个段都包含同一内容的替代版本 (在本例中为一个文本版本和一个 HTML 版本)。如果收件人的电子邮件客户端可以显示 HTML 内容,则它显示邮件正文的 HTML 版本。如果收件人的电子邮件客户端无法显示 HTML 内容,则它显示邮件正文的纯文本版本。邮件的两个版本还将包含一个附件(在本例中为包含一些客户名称的短文本文件)。

当您将一个 MIME 段嵌套在另一个段中时 (如本示例所示),嵌套的段必须使用与父段中的 boundary 参数不同的 boundary 参数。这些边界应该是唯一的字符串。要定义 MIME 段之间的边界,请键入两个连字符 (–) 后跟边界字符串。在 MIME 段的末尾,在边界字符串的开头和末尾处放置两个连字符

MIME 编码

为了保持与旧系统的兼容性,Amazon SES 遵循了 SMTP 的 7 位 ASCII 限制,具体如RFC 2821。如果要发送包含非 ASCII 字符的内容,则必须将这些字符编码为使用 7 位 ASCII 字符的格式。

电子邮件地址

要对邮件信封中使用的电子邮件地址进行编码,请使用 Punycode 编码。

例如,要向 张伟@example.com 发送电子邮件,请对地址的本地部分(@ 符号前的部分)使用 Punycode 编码。生成的编码地址为 xn–cpqy30b@example.com。

注意
此规则仅适用于您在邮件信封中指定的电子邮件地址,不适用于邮件标头。使用 SendRawEmail API 时,您在 Source 和 Destinations 参数中指定的地址分别定义信封发件人和收件人。

电子邮件标头

要对邮件标头进行编码,请使用 MIME encoded-word 语法。MIME encoded-word 语法使用以下格式:

文件附件

Content-Type:附件的文件类型。以下是常见 MIME Content-Type 声明的示例:

纯文本文件:Content-Type: text/plain; name=“sample.txt”

Microsoft Word 文档:Content-Type: application/msword; name=“document.docx”

JPG 图像:Content-Type: image/jpeg; name=“photo.jpeg”

Content-Disposition:指定收件人的电子邮件客户端应如何处理内容。对于附件,此值为 Content-Disposition: attachment。

Content-Transfer-Encoding:用于对附件进行编码的方案。对于文件附件,此值几乎总是 base64。

Amazon SES 接受最常见的文件类型。有关 Amazon SES 不接受的文件类型的列表,请参阅不支持的附件类型。

使用 Amazon SES API 发送原始电子邮件

package com.amazonaws.samples;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.util.Properties;// JavaMail libraries. Download the JavaMail API
// from https://javaee.github.io/javamail/
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;// AWS SDK libraries. Download the AWS SDK for Java
// from https://aws.amazon.com/sdk-for-java
import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
import com.amazonaws.services.simpleemail.model.RawMessage;
import com.amazonaws.services.simpleemail.model.SendRawEmailRequest;public class AmazonSESSample {// Replace sender@example.com with your "From" address.// This address must be verified with Amazon SES.private static String SENDER = "Sender Name <sender@example.com>";// Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified.private static String RECIPIENT = "recipient@example.com";// Specify a configuration set. If you do not want to use a configuration// set, comment the following variable, and the // ConfigurationSetName=CONFIGURATION_SET argument below.private static String CONFIGURATION_SET = "ConfigSet";// The subject line for the email.private static String SUBJECT = "Customer service contact info";// The full path to the file that will be attached to the email.// If you're using Windows, escape backslashes as shown in this variable.private static String ATTACHMENT = "C:\\Users\\sender\\customers-to-contact.xlsx";// The email body for recipients with non-HTML email clients.private static String BODY_TEXT = "Hello,\r\n"+ "Please see the attached file for a list "+ "of customers to contact.";// The HTML body of the email.private static String BODY_HTML = "<html>"+ "<head></head>"+ "<body>"+ "<h1>Hello!</h1>"+ "<p>Please see the attached file for a "+ "list of customers to contact.</p>"+ "</body>"+ "</html>";public static void main(String[] args) throws AddressException, MessagingException, IOException {Session session = Session.getDefaultInstance(new Properties());// Create a new MimeMessage object.MimeMessage message = new MimeMessage(session);// Add subject, from and to lines.message.setSubject(SUBJECT, "UTF-8");message.setFrom(new InternetAddress(SENDER));message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(RECIPIENT));// Create a multipart/alternative child container.MimeMultipart msg_body = new MimeMultipart("alternative");// Create a wrapper for the HTML and text parts.        MimeBodyPart wrap = new MimeBodyPart();// Define the text part.MimeBodyPart textPart = new MimeBodyPart();textPart.setContent(BODY_TEXT, "text/plain; charset=UTF-8");// Define the HTML part.MimeBodyPart htmlPart = new MimeBodyPart();htmlPart.setContent(BODY_HTML,"text/html; charset=UTF-8");// Add the text and HTML parts to the child container.msg_body.addBodyPart(textPart);msg_body.addBodyPart(htmlPart);// Add the child container to the wrapper object.wrap.setContent(msg_body);// Create a multipart/mixed parent container.MimeMultipart msg = new MimeMultipart("mixed");// Add the parent container to the message.message.setContent(msg);// Add the multipart/alternative part to the message.msg.addBodyPart(wrap);// Define the attachmentMimeBodyPart att = new MimeBodyPart();DataSource fds = new FileDataSource(ATTACHMENT);att.setDataHandler(new DataHandler(fds));att.setFileName(fds.getName());// Add the attachment to the message.msg.addBodyPart(att);// Try to send the email.try {System.out.println("Attempting to send an email through Amazon SES "+"using the AWS SDK for Java...");// Instantiate an Amazon SES client, which will make the service // call with the supplied AWS credentials.AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard()// Replace US_WEST_2 with the AWS Region you're using for// Amazon SES..withRegion(Regions.US_WEST_2).build();// Print the raw email content on the consolePrintStream out = System.out;message.writeTo(out);// Send the email.ByteArrayOutputStream outputStream = new ByteArrayOutputStream();message.writeTo(outputStream);RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage).withConfigurationSetName(CONFIGURATION_SET);client.sendRawEmail(rawEmailRequest);System.out.println("Email sent!");// Display an error if something goes wrong.} catch (Exception ex) {System.out.println("Email Failed");System.err.println("Error message: " + ex.getMessage());ex.printStackTrace();}}
}

参考链接:https://docs.aws.amazon.com/zh_cn/ses/latest/DeveloperGuide/send-email-raw.html

使用 Amazon SES API 发送原始电子邮件相关推荐

  1. php ses 发送邮件,使用 Amazon SES API 和 AWS SDK for PHP 版本 3 验证电子邮件身份 - 适用于 PHP 的 AWS 开发工具包...

    AWS 文档中描述的 AWS 服务或功能可能因区域而异.要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门. 本文属于机器翻译版本.若本译文内容与英语原文存在差异,则一律以英文原文为准. 使用 ...

  2. Amazon SES 邮件发送服务

    1. 进入AWS后台,在服务中找到SES(Simple Email Service)点击. 2. 进入SES主页,点击左侧导航栏中"SMTP Settings". 3. 进入&qu ...

  3. 免费超大量邮件发送服务Amazon SES和Mailgun提供SMTP和API支持

    一般来说网站注册.论坛消息.新闻推送.广告宣传等都会有发送邮件服务,大量的邮件发送服务如果用PHP来发送,一是会消耗主机资源,二是容易被各大邮箱判定为垃圾邮件而被拒收.用第三方的邮局服务发送邮件,可以 ...

  4. php统计邮件打开率,监控 Amazon SES 电子邮件的打开率、点击率和退回率

    如何监控通过使用 Amazon SES 发送的电子邮件的打开率.点击率和退回率? 上次更新时间:2020 年 12 月 14 日 我想要获得我使用 Amazon Simple Email Servic ...

  5. Amazon SES介绍 - 服务特点

         什么是Amazon SES       Amazon SES,  全称Amazon Simple Email Service,即Amazon简单邮件服务,它是Amazon提供的一款供开发人员 ...

  6. 亚马逊ses如何发qq_使用Amazon SES发送电子邮件

    亚马逊ses如何发qq by Kangze Huang 黄康泽 使用Amazon SES发送电子邮件 (Sending emails with Amazon SES) 完整的AWS Web样板-教程3 ...

  7. java 使用 Amazon SES 发送电子邮件

    java 使用 Amazon SES发送电子邮件,有三种方式,具体哪三种请移步这里. 这里只介绍一种:利用Amazon SES 的 SMTP 服务,javax.mail发送邮件.此方法也可以直接用于1 ...

  8. php ses 发送邮件,Amazon SES – 通过PHP sdk发送HTML邮件

    我正在尝试使用amazon ses PHP sdk发送电子邮件. 我得到了以下代码.工作得很好 $body = " HTML email This email contains HTML T ...

  9. php ses 发送邮件,使用PHP SDK从Amazon SES发送HTML邮件

    我正在开发一项从AWS SES服务发送电子邮件的服务.我已经能够发送纯文本邮件,但我需要在其中一个案例中发送丰富的 HTML邮件.这是我使用的代码: header("MIME-Version ...

最新文章

  1. PHP数组的交集array_intersect(),array_intersect_assoc(),array_inter_key()函数的小问题
  2. python编程基础教程-史上最全Python编程基础入门教程
  3. MemoryMappingFile泄漏分析过程
  4. 语言 山东专升本 真题_专升本英语真题
  5. IT Monitor
  6. 告诉你,Spring Boot 真是个牛逼货
  7. C++:画数组元素直方图
  8. python int32 int8_python-使用numpy视图将int32转换为int8
  9. 别熬夜加班了,Facebook 开源了一款代码推荐神器!| 程序员硬核评测
  10. python的基础是java_Python基础学习
  11. 如何执行静默(无人参与)Java 安装?
  12. C语言基础教程之常量
  13. vue 登录页面记住密码功能
  14. 修正win10部署JDK8(使用JDK文件夹中的自带JRE)
  15. graphpad如何换柱状图与折线图能否混合一起_excel柱状图加折线图组合怎么做,原来是这样的...
  16. 【图像分类数据集】非常全面实用的垃圾分类图片数据集共享
  17. 计算机国内期刊sci,国内计算机类期刊 SCI收录:
  18. 圣多米尼克高中有计算机课吗,院校库_VPEA北美留学院校数据查询中心
  19. 微信小程序中 三元运算符的嵌套使用
  20. daterangepicker 清空_Date Range Picker 中文网

热门文章

  1. ARM7、ARM9、ARM11区别
  2. 【信号与系统】(二十二)拉普拉斯变换与复频域分析——s域分析
  3. 大前端开发 前端如何开发 APP
  4. JAVA编程习题及答案_完美版
  5. IP数据包的路由转发
  6. Google chrome插件 | pagenote 网页标记
  7. http chunk
  8. DataStream API【3】
  9. WEB 请求处理二:Nginx 请求 反向代理
  10. 第一章 常用半导体器件-----------------本征半导体