1.把邮件保存为一个文件,这个文件可以直接用Outlook,Foxmail等邮件客户端工具打开。
private void savemail(MimeMessage msg, String path) {
try {
File f = new File(path);
f.getParentFile().mkdirs();
FileOutputStream fo = new FileOutputStream(f);
try {
File f = new File(path);
f.getParentFile().mkdirs();
FileOutputStream fo = new FileOutputStream(f);
//CRLFOutputStream 可以把一些换行符不满足MIME规范的邮件进行修正。
CRLFOutputStream CRLFO = new CRLFOutputStream(fo);
msg.writeTo(CRLFO);
CRLFO.close();
fo.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
msg.writeTo(CRLFO);
CRLFO.close();
fo.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
调用:
savemail(msg,"c:/lizongbo/testmail.eml");
2.对邮件体使用base64编码。
默认情况下使用的是quoted-printable编码:
示例如下:
MimeMessage msg = new MimeMessage((Session)null);
msg.setFrom(new InternetAddress("lizongbo@gmail.com"));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress("lizongbo@msn.com"));
msg.setText("测试一下,邮件来自 http://www.donews.net/lizongbo !!!");
msg.setSubject("测试标题!!!", "GB2312");
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart txtbodyPart = new MimeBodyPart();
txtbodyPart.setText("这是一封html邮件,请用html方式察看!!!");
multipart.addBodyPart(txtbodyPart);
MimeBodyPart htmlodyPart = new MimeBodyPart();
String content="html邮件内容!来自 http://www.donews.net/lizongbo ";
content = "<html><body>" content "</body><html>";
htmlodyPart.setContent(content, "text/html;charset=GBK");
multipart.addBodyPart(htmlodyPart);
msg.setContent(multipart);
msg.saveChanges();
msg.setFrom(new InternetAddress("lizongbo@gmail.com"));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress("lizongbo@msn.com"));
msg.setText("测试一下,邮件来自 http://www.donews.net/lizongbo !!!");
msg.setSubject("测试标题!!!", "GB2312");
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart txtbodyPart = new MimeBodyPart();
txtbodyPart.setText("这是一封html邮件,请用html方式察看!!!");
multipart.addBodyPart(txtbodyPart);
MimeBodyPart htmlodyPart = new MimeBodyPart();
String content="html邮件内容!来自 http://www.donews.net/lizongbo ";
content = "<html><body>" content "</body><html>";
htmlodyPart.setContent(content, "text/html;charset=GBK");
multipart.addBodyPart(htmlodyPart);
msg.setContent(multipart);
msg.saveChanges();
得到的邮件内容为:
--------------------------------------------------------------
Message-ID: <32591083.1110043294640.JavaMail.lizongbo@localhost>
From: lizongbo@gmail.com
To: lizongbo@msn.com
Subject: =?GB2312?B?suLK1LHqzOKjoaOho6E=?=
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_Part_0_8568863.1110043294484"
------=_Part_0_8568863.1110043294484
Content-Type: text/plain; charset=GBK
Content-Transfer-Encoding: base64
1eLKx9K7t JodG1s08q8/qOsx vTw2h0bWy3VCq9suy/tKOho6GjoQ==
------=_Part_0_8568863.1110043294484
Content-Type: text/html;charset=GBK
Content-Transfer-Encoding: quoted-printable
<html><body>html=D3=CA=BC=FE=A3=A1 http://www.donews.net/lizongbo </body><h=
tml>
------=_Part_0_8568863.1110043294484--
--------------------------------------------------------------
使用base64编码的代码:
--------------------------------------------------------------
MimeMessage msg = new MimeMessage( (Session)null);
msg.setFrom(new InternetAddress("lizongbo@gmail.com"));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress("lizongbo@msn.com"));
msg.setText("测试一下,邮件来自 http://www.donews.net/lizongbo !!!");
msg.setSubject("测试标题!!!", "GB2312");
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart txtbodyPart = new MimeBodyPart();
txtbodyPart.setText("这是一封html邮件,请用html方式察看!!!");
multipart.addBodyPart(txtbodyPart);
MimeBodyPart htmlodyPart = new MimeBodyPart();
String content = "html邮件! http://www.donews.net/lizongbo ";
content = "<html><body>" content "</body><html>";
htmlodyPart.setContent(content, "text/html;charset=GBK");
//最最关键的就这么一行
htmlodyPart.setHeader("Content-Transfer-Encoding", "base64");
multipart.addBodyPart(htmlodyPart);
msg.setContent(multipart);
msg.saveChanges();
msg.setFrom(new InternetAddress("lizongbo@gmail.com"));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress("lizongbo@msn.com"));
msg.setText("测试一下,邮件来自 http://www.donews.net/lizongbo !!!");
msg.setSubject("测试标题!!!", "GB2312");
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart txtbodyPart = new MimeBodyPart();
txtbodyPart.setText("这是一封html邮件,请用html方式察看!!!");
multipart.addBodyPart(txtbodyPart);
MimeBodyPart htmlodyPart = new MimeBodyPart();
String content = "html邮件! http://www.donews.net/lizongbo ";
content = "<html><body>" content "</body><html>";
htmlodyPart.setContent(content, "text/html;charset=GBK");
//最最关键的就这么一行
htmlodyPart.setHeader("Content-Transfer-Encoding", "base64");
multipart.addBodyPart(htmlodyPart);
msg.setContent(multipart);
msg.saveChanges();
得到邮件内容如下:
--------------------------------------------------------------
Message-ID: <33109165.1110043370875.JavaMail.lizongbo@localhost>
From: lizongbo@gmail.com
To: lizongbo@msn.com
Subject: =?GB2312?B?suLK1LHqzOKjoaOho6E=?=
Mime-Version: 1.0
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



