通过SMTP发送带附件,纯文本/文本和文本/ html的电子邮件

时间:2013-06-13 21:53:43

标签: email smtp javamail html-email email-attachments

我的目标:通过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附件发送。邮件正文不显示,附件重复。

enter image description here

相同的消息但具有不同的附件(.gif)。一切都很好看。 enter image description here

=== 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());
                    }
                }
            }

1 个答案:

答案 0 :(得分:11)

您的邮件结构错误。您需要嵌套的多部分才能获得正确的结构,如下所示:

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