簡體   English   中英

gmail 為何顯示使用通過 Amazon SES 發送的 MimeMessageHelper addInline 附加的圖像的“無名”附件?

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

徽標文件夾:

在此處輸入圖像描述

Email 附件: 在此處輸入圖像描述

我知道現在回答這個問題有點晚了。 我最近在使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM