我目前正在使用 AWS SDK 发送邮件:
// from: [email protected]
public void sendMail(String from, String to, String subject, String htmlBody) {
LOGGER.info(String.format("Sending Mail from: %s, to: %s, with subject: %s", from, to, subject));
try {
AWSCredentials credentials = new BasicAWSCredentials(awsAccessKey, awsSecret);
AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard()
.withRegion(Regions.AP_SOUTH_1).withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
SendEmailRequest request = new SendEmailRequest().withDestination(new Destination().withToAddresses(to))
.withMessage(new Message()
.withBody(new Body().withHtml(new Content().withCharset("UTF-8").withData(htmlBody)))
.withSubject(new Content().withCharset("UTF-8").withData(subject)))
.withSource(from);
client.sendEmail(request);
} catch (Exception e) {
LOGGER.error(String.format("Error while sending email: %s", e.getMessage()));
throw new IllegalArgumentException(MessageEnum.FAILED_TO_SEND_AN_EMAIL.name());
}
}
有没有人知道如何发送包含发件人姓名的电子邮件?任何帮助表示赞赏。
如果您查看文档,SendEmailRequest
您会发现它支持在电子邮件中定义友好名称的标准方式:
source - 发送电子邮件的电子邮件地址。此电子邮件地址必须通过 Amazon SES 单独验证,或来自已通过 Amazon SES 验证的域。有关验证身份的信息,请参阅 Amazon SES 开发人员指南。如果您代表另一个用户发送并且已被发送授权策略允许这样做,则您还必须指定 SourceArn 参数。有关发送授权的更多信息,请参阅 Amazon SES 开发人员指南。Amazon SES 不支持 SMTPUTF8 扩展,如 RFC6531 中所述。因此,源电子邮件地址的本地部分(@ 符号之前的电子邮件地址部分)可能仅包含 7 位 ASCII 字符。如果地址的域部分(@ 符号之后的部分)包含非 ASCII 字符,它们必须使用 Punycode 进行编码,如 RFC3492 中所述。发件人名称(也称为友好名称)可能包含非 ASCII 字符。这些字符必须使用 MIME 编码字语法进行编码,如 RFC 2047 中所述。MIME 编码字语法使用以下形式:=?charset?encoding?encoded-text?=。
因此该字符串应如下所示: John Doe <[email protected]>
InternetAddress
可以用于此,但您需要注意SendEmailRequest
具有 forfrom
参数的编码限制
request.withSource((new InternetAddress("[email protected]", "Your Name")).toString());
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句