简体   繁体   中英

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. These images seem to be random unused images from the same folder.

AddInLine code

    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

            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:

<!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:

在此处输入图像描述

Email attachments: 在此处输入图像描述

I know it's a bit late to answer this question. I recently also experienced this issue while sending email using AWS SES. The reason is that the MimeMessageHelper does not set the filename for our inline images when it creates BodyPart from our images. The contentId it uses is twisted a bit with the prefix < and suffix > . We may retrieve the corresponding BodyPart instance from the helper class by the following code:

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);

        }
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM