通过 SMTP 发送带有附件、纯文本/文本和文本/hml 的电子邮件 [英] Send email via SMTP with attachment, plain/text, and text/hml

查看:102
本文介绍了通过 SMTP 发送带有附件、纯文本/文本和文本/hml 的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My goal: Send transactional emails via SMTP with plain/text, text/html, and attachments.

我的代码:用JavaMail实现

My code: Implemented with JavaMail

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

My issue: It looks fine on hotmail, or outlook. But on gmail, it does not show the message body properly if it is an email with a .txt attachment (it works alright if attachments are images)

任何帮助将不胜感激.

这是我的原始 SMTP 输出:

Here is my raw SMTP output:

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 附件.邮件正文不显示,附件重复.

Sent with only one .txt attachment. The message body does not display and attachment are duplicated.

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

Same message but with different attachment (.gif). Everything looks fine.

=== Java 开发人员的解决方案 ====

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

所以,现在我的代码看起来像:

So, now my code looks like:

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

推荐答案

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

The structure of your message is wrong. You need nested multiparts to get the right structure, something like this:

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

这篇关于通过 SMTP 发送带有附件、纯文本/文本和文本/hml 的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆