[英]How come gmail is showing 'noname' attachments for images attached using MimeMessageHelper addInline sent with Amazon SES?
When using Spring MimeMessageHelper to attach the icons using addInLine and sending with SES, some of the icons appear as 'noname' attachments.当使用 Spring MimeMessageHelper 使用 addInLine 附加图标并使用 SES 发送时,一些图标显示为“无名”附件。 These images seem to be random unused images from the same folder.
这些图像似乎是同一文件夹中随机未使用的图像。
AddInLine code 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 code 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/");
Sendmail code:发送邮件代码:
// 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;
Parts of html code: 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>
Logo Folder:徽标文件夹:
I know it's a bit late to answer this question.我知道现在回答这个问题有点晚了。 I recently also experienced this issue while sending email using AWS SES.
我最近在使用 AWS SES 发送 email 时也遇到了这个问题。 The reason is that the
MimeMessageHelper
does not set the filename for our inline images when it creates BodyPart
from our images.原因是
MimeMessageHelper
在从我们的图像创建BodyPart
时没有为我们的内联图像设置文件名。 The contentId
it uses is twisted a bit with the prefix < and suffix > .它使用的
contentId
带有前缀<和后缀> 。 We may retrieve the corresponding BodyPart
instance from the helper class by the following code:我们可以通过以下代码从帮助程序 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.