[英]How come gmail is showing 'noname' attachments for images attached using MimeMessageHelper addInline sent with Amazon SES?
當使用 Spring MimeMessageHelper 使用 addInLine 附加圖標並使用 SES 發送時,一些圖標顯示為“無名”附件。 這些圖像似乎是同一文件夾中隨機未使用的圖像。
AddInLine 代碼
private void attachIconsInEmailBody(MimeMessageHelper messageHelper, String iconsPath) throws IOException, MessagingException {
Resource[] resources = resourcePatternResolver.getResources("classpath:" + iconsPath + "*.*");
for (Resource attRes: resources) {
String iconName = attRes.getFilename();
messageHelper.addInline(iconName.split("\\.")[0], attRes);
}
Email代碼
MimeMessage message = new MimeMessage(session);
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");
messageHelper.setFrom(new InternetAddress(emailObject.getSender()));
messageHelper.setTo(InternetAddress.parse(emailObject.getRecipients()));
messageHelper.setSubject(emailObject.getSubject());
messageHelper.setText(htmlBody, true);
messageHelper.setSentDate(new Date(System.currentTimeMillis()));
// add all the icons in-line to display in the email body
attachIconsInEmailBody(messageHelper,"static/logo/SG/");
發送郵件代碼:
// send the email using the sendRawEmail API
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
message.writeTo(outputStream);
RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage);
SendRawEmailResult emailResult = sesClient.sendRawEmail(rawEmailRequest);
// set the response back in the email object for any further recon
emailObject.setEmailResult(emailResult);
outputStream.close();
return emailResult;
html部分代碼:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<td style="width: 40px; height: 120px;"><img
src="cid:strauss_sg_blank1" style="height: 120px; width: 40px;"
alt="strauss_sg_blank1">
</td>
<td valign="top"
style="width: 40px; height: 30px; background: #ffffff;"><img
src="cid:strauss_sg_blank2" style="height: 30px"
alt="strauss_sg_blank2"></td>
</body>
</html>
徽標文件夾:
我知道現在回答這個問題有點晚了。 我最近在使用 AWS SES 發送 email 時也遇到了這個問題。 原因是MimeMessageHelper
在從我們的圖像創建BodyPart
時沒有為我們的內聯圖像設置文件名。 它使用的contentId
帶有前綴<和后綴> 。 我們可以通過以下代碼從幫助程序 class 中檢索相應的BodyPart
實例:
private void attachIconsInEmailBody(MimeMessageHelper messageHelper, String iconsPath) throws IOException, MessagingException {
Resource[] resources = resourcePatternResolver.getResources("classpath:" + iconsPath + "*.*");
for (Resource attRes: resources) {
String iconName = attRes.getFilename();
String contentId = iconName.split("\\.")[0];
messageHelper.addInline(contentId , attRes);
messageHelper.getMimeMultipart().getBodyPart("<" + contentId + ">").setFileName(iconName);
}
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.