amazon-web-services - 带有附件的 AWS-Pinpoint 电子邮件通知不起作用
问题描述
我正在尝试做一个 POC,其中我使用 aws pinpoint 发送电子邮件。简单的电子邮件工作正常,但是当我尝试发送带有附件的电子邮件时,我无法确定什么是正确的方法。在文档中,此链接描述了需要做什么
https://docs.aws.amazon.com/pinpoint-email/latest/APIReference/API_RawMessage.html
以下是我在各种网站上找到的代码:
// Create a new email client
AmazonPinpointEmail client = AmazonPinpointEmailClientBuilder.standard()
.withRegion(region).build();
// Combine all of the components of the email to create a request.
SendEmailRequest request = new SendEmailRequest()
.withFromEmailAddress(senderAddress)
.withConfigurationSetName(configurationSet)
.withDestination(new Destination()
.withToAddresses(toAddresses)
.withCcAddresses(ccAddresses)
.withBccAddresses(bccAddresses)
)
.withContent(new EmailContent()
.withRaw(new RawMessage().withData())
//the withData takes type buffer, how to create a message which contains attachement.
client.sendEmail(request);
System.out.println("Email sent!");
System.out.println(request);
任何人使用此 api 发送附件,请帮助创建包含附件、主题和正文的消息。谢谢
解决方案
要使用 Amazon PinpointEmail 发送带有附件的电子邮件,您需要(为简单起见):
- JavaMail 库:一个 API,用于通过利用BodyPart、MimeBodyPart等类来编写、编写和读取电子消息
以下是我测试过的示例 java 代码片段:
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(senderAddress));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
// 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 attachment
MimeBodyPart 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("===============================================");
System.out.println("Getting Started with Amazon PinpointEmail"
+"using the AWS SDK for Java...");
System.out.println("===============================================\n");
// Instantiate an Amazon PinpointEmail client, which will make the service call with the supplied AWS credentials.
AmazonPinpointEmail client = AmazonPinpointEmailClientBuilder.standard()
.withRegion(Regions.US_EAST_1).build();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
message.writeTo(outputStream);
SendEmailRequest rawEmailRequest = new SendEmailRequest()
.withFromEmailAddress(senderAddress)
.withDestination(new Destination()
.withToAddresses(toAddress)
)
.withContent(new EmailContent()
.withRaw(new RawMessage().withData(ByteBuffer.wrap(outputStream.toByteArray())))
);
client.sendEmail(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();
}
上面的代码可以总结为6个步骤:
- 获取会话
- 创建 MimeBodyPart 对象
- 创建 MimeMultiPart 对象
- 创建数据源(定义附件)
- 将部分添加到 MimeMultiPart
- 通过 AmazonPinpointEmail API 发送电子邮件
你可以在github中找到完整的代码
希望这可以帮助。
推荐阅读
- c - 查找在 ELF 文件中使用符号的用户
- verilog - 可以在systemverilog中的generate for loop内部使用动态数组吗
- c# - 一种在 ASP.NET Core 中的集成测试,使用 EF 和 AutoMapper
- python - 使用python中fuzzywuzzy中的extractOne()与字符串列表匹配的错误字符串
- dart - 在 Flutter 中创建圆形缓存图像
- c# - 在调试器快照中隐藏敏感数据
- javascript - undefined 不是对象(评估“this.props.navigation”)
- r - jupyter中的r图-无法启动png()设备
- react-native - React Navigation - 如何在屏幕之间重复传递数据?
- path - Windows 8.1 中意外删除的路径变量