javamail的几个实用知识点
2008-02-23 10:06:41来源:互联网 阅读 ()
private void savemail(MimeMessage msg, String path) {
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();
}
}
调用:
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();
得到的邮件内容为:
--------------------------------------------------------------
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();
得到邮件内容如下:
--------------------------------------------------------------
Message-ID: <33109165.1110043370875.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.1110043370687"
------=_Part_0_8568863.1110043370687
Content-Type: text/plain; charset=GBK
Content-Transfer-Encoding: base64
1eLKx9K7t JodG1s08q8/qOsx vTw2h0bWy3vcq9suy/tKOho6GjoQ==
------=_Part_0_8568863.1110043370687
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:初探关键字volatile
下一篇:Junit FAQ(017)
- javamail的几个实用知识点. 2008-02-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash