【问题标题】:Send email via SMTP with attachment, plain/text, and text/hml通过 SMTP 发送带有附件、纯文本/文本和文本/html 的电子邮件
【发布时间】:2013-06-10 11:20:18
【问题描述】:

我的目标:通过 SMTP 发送带有纯文本、文本/html 和附件的交易电子邮件。

我的代码:用JavaMail实现

我的问题: 在 hotmail 或 Outlook 上看起来不错。但是在 gmail 上,如果是带有 .txt 附件的电子邮件,它不会正确显示邮件正文(如果附件是图像,它可以正常工作)

任何帮助将不胜感激。

这是我的原始 SMTP 输出:

Subject: ALTERNATIVE | TXT | HTML |ATT.ATTACHMENT | Thu Jun 13 17:48:04 EDT
 2013
MIME-Version: 1.0
Content-Type: multipart/alternative; 
    boundary="----=_Part_0_21791733.1371160084561"

------=_Part_0_21791733.1371160084561
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Body message in text format!
------=_Part_0_21791733.1371160084561
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

Body message in <b>html</b> format! Sent on Thu Jun 13 17:48:04 EDT 2013<br> to: me@gmail.com<br> to: me@mijo.com<br> cc: me@hotmail.com<br> cc: rafael.santos.test@hotmail.com
------=_Part_0_21791733.1371160084561
Content-Type: text/plain; charset=us-ascii; name=email_attachment.txt
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=email_attachment.txt

This is a text attachment file!
------=_Part_0_21791733.1371160084561--
.
250 Delivery in progress
QUIT

一些截图

仅使用一个 .txt 附件发送。邮件正文不显示,附件重复。

相同的消息,但带有不同的附件 (.gif)。一切看起来都很好。

=== 面向 JAVA 开发人员的解决方案 ====

这里描述了整体思路:http://www.coderanch.com/t/503380/java/java/Java-Mail-text-html-attachment

所以,现在我的代码如下所示:

// contentPart is the content to be sent. It is divided in bodyContent and attachmentContent
            MimeMultipart contentPart = new MimeMultipart("mixed");

            // Message body in txt and html format
            MimeMultipart bodyPart = new MimeMultipart("alternative");
            // Creates plain text message
            BodyPart bodyTxt = new MimeBodyPart();
            bodyTxt.setText(getMessageBodyText());
            // Creates html message
            BodyPart bodyHtml = new MimeBodyPart();
            bodyHtml.setContent(getMessageBodyHtml(), "text/html");
            bodyPart.addBodyPart(bodyTxt);
            bodyPart.addBodyPart(bodyHtml);

            // Wrapper for bodyTxt and bodyHtml
            MimeBodyPart bodyContent = new MimeBodyPart();
            bodyContent.setContent(bodyPart);

            // At this point, contentPart contains bodyTxt and bodyHtml wrapped in a multipart/alternative
            contentPart.addBodyPart(bodyContent);

            // Adds attachments to contentPart
            if (getAttachments() != null) {
                for(File f : getAttachments()) {
                    try {
                        MimeBodyPart attachmentPart = new MimeBodyPart();
                        attachmentPart.attachFile(f);
                        contentPart.addBodyPart(attachmentPart);
                    } catch (IOException e) {
                        logger.severe("Could not attach file to email!" +
                                " TO: "+ getTo().toString() +
                                "; CC: "+ getCc().toString() +
                                "; ExceptionMessage: " + e.getMessage());
                        throw new SmtpRequestException(e.getMessage());
                    }
                }
            }

【问题讨论】:

    标签: email smtp jakarta-mail html-email email-attachments


    【解决方案1】:

    您的信息结构错误。您需要嵌套的多部分来获得正确的结构,如下所示:

      multipart/mixed
        multipart/alternative (holding the two forms of the body part)
          text/plain
          text/html
        text/plain or image/gif (the attachment)
    

    【讨论】:

    • 没错。实际上,在看到你的答案之前,我已经实现了这个问题的解决方案。非常感谢。
    • 如果某些附件实际上是图片,嵌入在 text/html 部分怎么办?那么,正确的结构是什么样的呢?我可以简单地将“混合”替换为“相关”吗?或者我是否必须对具有内联处置的“相关”附件使用不同的子部分,而对其余部分使用“混合”?我很困惑... :(
    • 保持混合。此处提供的答案非常适用于文本和二进制文件。
    • 如果 text/html 部分使用“cid:”引用引用图像,则它们都应该在 multipart/related 中。不要替换上面示例中的 multipart/mixed。相反,将 text/html 替换为 multipart/related,其中包括它引用的 text/html 和 image/jpg。如果不清楚,请告诉我。
    • Multipart mp = new MimeMultipart(); Multiaprt mpa = new MimeMultipart("alternative"); MimeBodyPart mbp = new MimeBodyPart(); mbp.setContent(mpa); mp.addBodyPart(mbp);
    猜你喜欢
    • 2023-03-26
    • 2016-03-13
    • 2013-07-26
    • 2012-07-14
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多