通过SMTP,附件,plain / text和text / hml发送电子邮件 [英] Send email via SMTP with attachment, plain/text, and text/hml
问题描述
我的目标:通过SMTP发送交易电子邮件,包含纯文本/文本,文本/ HTML和附件。
我的代码:使用JavaMail实现
我的问题:在hotmail或Outlook中看起来不错。但是,在 gmail 中,如果邮件正文是带有.txt附件的电子邮件,则不会正确显示邮件正文(如果附件是图像,则可以正常工作)
这是我的原始SMTP输出:
主题:ALTERNATIVE | TXT | HTML | ATT.ATTACHMENT | Thu Jun 13 17:48:04 EDT
2013
MIME版本:1.0
内容类型:multipart / alternative;
boundary =---- = _ Part_0_21791733.1371160084561
------ = _ Part_0_21791733.1371160084561
Content-Type:text / plain; charset = us-ascii
内容传输编码:7bit
文本格式的正文消息!
------ = _ Part_0_21791733.1371160084561
内容类型:text / html; charset = us-ascii
Content-Transfer-Encoding:7bit
< b> html< / b>中的正文消息格式!发表于六月13 17:48:04 EDT 2013< br>至:me@gmail.com br>至:me@mijo.com< br> cc:me@hotmail.com br> cc:rafael.santos.test@hotmail.com
------ = _ Part_0_21791733.1371160084561
内容类型:text / plain;字符集= US-ASCII; name = email_attachment.txt
Content-Transfer-Encoding:7bit
Content-Disposition:attachment; filename = email_attachment.txt
这是一个文本附件文件!
------ = _ Part_0_21791733.1371160084561--
。
250交付进行中
QUIT
某些屏幕截图
只发送一个.txt附件发送。消息正文不显示,附件被复制。
相同的消息,但具有不同的附件(.gif)。一切看起来都不错
=== JAVA开发人员的解决方案====
总体思路如下: http://www.coderanch.com/t/503380/ java / java / Java-Mail-text-html-attachment
所以现在我的代码如下:
// contentPart是要发送的内容。它分为bodyContent和attachmentContent
MimeMultipart contentPart = new MimeMultipart(mixed);
// txt和html格式的消息体
MimeMultipart bodyPart = new MimeMultipart(alternative);
//创建纯文本消息
BodyPart bodyTxt = new MimeBodyPart();
bodyTxt.setText(getMessageBodyText());
//创建html消息
BodyPart bodyHtml = new MimeBodyPart();
bodyHtml.setContent(getMessageBodyHtml(),text / html);
bodyPart.addBodyPart(bodyTxt);
bodyPart.addBodyPart(bodyHtml);
// bodyTxt和bodyHtml的包装器
MimeBodyPart bodyContent = new MimeBodyPart();
bodyContent.setContent(bodyPart);
//此时,contentPart包含以多部分/替代方式
contentPart.addBodyPart(bodyContent)包装的bodyTxt和bodyHtml;
//将附件添加到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());
抛出新的SmtpRequestException(e.getMessage());
}
}
}
您的邮件的结构是错误的。您需要嵌套多部分才能获得正确的结构,如下所示:
multipart / mixed
multipart / alternative两种形式的身体部分)
text / plain
text / html
text / plain或image / gif(附件)
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 <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
Some screenshots
Sent with only one .txt attachment. The message body does not display and attachment are duplicated.
Same message but with different attachment (.gif). Everything looks fine.
=== SOLUTION FOR JAVA DEVELOPERS ====
The overall idea is described here: 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,附件,plain / text和text / hml发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!