java获取text plain,通过SMTP,附件,plain / text和text / hml发送电子邮件

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

My code: Implemented with JavaMail

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)

Any help would be highly appreciated.

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 html format! Sent on Thu Jun 13 17:48:04 EDT 2013
to: me@gmail.com
to: me@mijo.com
cc: me@hotmail.com
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

Some screenshots

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

0KLAB.png

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

MpkJI.png

=== SOLUTION FOR JAVA DEVELOPERS ====

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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值