我有一个托管在 AWS 上的 Spring Boot 应用程序。我正在使用 AWS SES 来触发电子邮件。但我不知道如何使用 SES 附加图像。我使用 JAVA 作为应用程序源代码。数据存储在数据库中,但未发送电子邮件。:
public void sendEmail(String to, String subject, String body) throws MessagingException {
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
Message msg = new MimeMessage(session);
MimeMultipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
// the body content:
messageBodyPart.setContent(BODY, "text/html");
multipart.addBodyPart(messageBodyPart);
// the image:
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(
"Logo.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image_01>");
multipart.addBodyPart(messageBodyPart);
// add the multipart to the message:
msg.setContent(multipart);
// set the remaining values as usual:
try {
msg.setFrom(new InternetAddress(FROM, FROMNAME));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(SUBJECT);
Transport transport = session.getTransport();
try {
System.out.println("Sending...");
transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("Email sent!");
} catch (Exception ex) {
System.out.println("The email was not sent.");
ex.printStackTrace();
} finally {
transport.close();
}
}