java 通过亚马逊 ses 发送 HTML 电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13801730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:01:55  来源:igfitidea点击:

Sending HTML email through amazon ses

javaamazon-web-servicesamazon-ses

提问by Neeraj

I am sending bulk emails using amazon ses. My code is given below

我正在使用亚马逊 ses 发送批量电子邮件。我的代码如下

public void sendMail(String sender, LinkedList<String> recipients, String subject, String body) {
    Destination destination = new Destination(recipients);
    try {
        ACCESS_KEY = EmailSender.prop.getProperty("accessKey");
        SECRET_KEY = EmailSender.prop.getProperty("secretKey");

        Content subjectContent = new Content(subject);
        Content bodyContent = new Content(body);
        Body msgBody = new Body(bodyContent);
        Message msg = new Message(subjectContent, msgBody);

        SendEmailRequest request = new SendEmailRequest(sender, destination, msg);

        AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
        AmazonSimpleEmailServiceClient sesClient = new AmazonSimpleEmailServiceClient(credentials);
        SendEmailResult result = sesClient.sendEmail(request);

        System.out.println(result + "Email sent");  
    }catch(Exception e) {
        System.out.println("Exception from EmailSender.java. Email not send");
    }

Here I have given my html content as string to the variable "body".

在这里,我将我的 html 内容作为字符串提供给变量“ body”。

The mail sent successfully. But I got the html content as email. How to send html content in mail. What changes in the code will solve this issue?

邮件发送成功。但我收到了 html 内容作为电子邮件。如何在邮件中发送 html 内容。代码中的哪些更改将解决此问题?

回答by Gerard Mu?oz Martínez

From the Amazon SES developerGuide:

来自Amazon SES developerGuide

You should use the WithHtml method:

您应该使用 WithHtml 方法:

Content subjContent = new Content().withData("Test of Amazon SES");
Message msg = new Message().withSubject(subjContent);

// Include a body in both text and HTML formats
Content textContent = new Content().withData("Hello - I hope you're having a good day.");
Content htmlContent = new Content().withData("<h1>Hello - I hope you're having a good day.</h1>");
Body body = new Body().withHtml(htmlContent).withText(textContent);
msg.setBody(body);