[英]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.