在 AWS SES 服务的电子邮件中添加嵌入式图像 [英] Add embedded image in emails in AWS SES service

查看:28
本文介绍了在 AWS SES 服务的电子邮件中添加嵌入式图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 Java 应用程序,它可以发送电子邮件以指定电子邮件.在电子邮件中,我还想附上一些图片.

I am trying to write a Java app which can send emails to specify emails. In the email i also want to attach some pic.

请在下面找到我的代码:-

Please find my code below :-

public class AmazonSESSample {

    static final String FROM = "abc@gmail.com";
    static final String TO = "def@gmail.com";
    static final String BODY = "This email was sent through Amazon SES by using the AWS SDK for Java. hello";
    static final String SUBJECT = "Amazon SES test (AWS SDK for Java)";

    public static void main(String[] args) throws IOException {
        Destination destination = new Destination().withToAddresses(new String[] { TO });
        Content subject = new Content().withData(SUBJECT);
        Message msg = new Message().withSubject(subject);
        // 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("<h2>Hi User,</h2>\n"
                + " <h3>Please find the ABC Association login details below</h3>\n"
                + " <img src=\"logo.png\" alt=\"Mountain View\">\n"
                + " Click <a href=\"http://google.com">here</a> to go to the association portal.\n"
                + " <h4>Association ID - 12345</h4>\n" + "  <h4>Admin UID - suny342</h4>\n"
                + " <h4>Password - poass234</h4>\n" + " Regards,\n" + " <br>Qme Admin</br>");
        Body body = new Body().withHtml(htmlContent);
        msg.setBody(body);
        SendEmailRequest request = new SendEmailRequest().withSource(FROM).withDestination(destination)
                .withMessage(msg);
        try {
            System.out.println("Attempting to send an email through Amazon SES by using the AWS SDK for Java...");
            AWSCredentials credentials = null;
            credentials = new BasicAWSCredentials("ABC", "CDF");
            try {
                // credentialsProvider.
            } catch (Exception e) {
                throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                        + "Please make sure that your credentials file is at the correct "
                        + "location (/Users/iftekharahmedkhan/.aws/credentials), and is in valid format.", e);
            }
            AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard()
                    .withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion("us-west-2").build();
            client.sendEmail(request);
            System.out.println("Email sent!");
        } catch (Exception ex) {
            System.out.println("The email was not sent.");
            System.out.println("Error message: " + ex.getMessage());
        }
    }
}

图像位于资源目​​录中,但未嵌入电子邮件中.任何人都可以帮忙.

The image is placed in the resource directory but it is not being embeded in the email. Can anyone please help.

推荐答案

代替相对路径,您需要使用图像本身的绝对公共路径或数据 URL.例如:

Instead of relative path, you'll need to use either an absolute public path to the image itself or a data URL. For example:

<img src=\"https://example.com/logo.png\" alt=\"Mountain View\" />

<img src=\"data:image/png;base64, {BASE64_ENCODED_DATA}\" alt=\"Mountain View\" />

编辑

截至 2020 年 1 月,Gmail 仍不支持 base64 编码的图像.

As of January 2020, Gmail still does not support base64 encoded images.

这篇关于在 AWS SES 服务的电子邮件中添加嵌入式图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆