12

我的目标:通过 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());
                    }
                }
            }
4

1 回答 1

12

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

  multipart/mixed
    multipart/alternative (holding the two forms of the body part)
      text/plain
      text/html
    text/plain or image/gif (the attachment)
于 2013-06-14T18:49:18.883 回答