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

Raf*_*afa 11 email smtp jakarta-mail html-email email-attachments

我的目标:使用普通/文本,文本/ html和附件通过SMTP发送交易电子邮件.

我的代码:用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
Run Code Online (Sandbox Code Playgroud)

一些截图

仅使用一个.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());
                    }
                }
            }
Run Code Online (Sandbox Code Playgroud)

Bil*_*non 11

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

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

  • 如果text/html部分使用"cid:"引用引用图像,则它们都应该是多部分/相关的.请勿替换上例中的multipart/mixed.相反,将text/html替换为包含text/html和它引用的image/jpg的multipart/related.如果不清楚,请告诉我. (2认同)