1.把邮件保存为一个文件,这个文件可以直接用outlook,foxmail等邮件客户端工具打开。
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
content-type: text/html;charset=gbk
content-transfer-encoding: base64
pgh0bww+pgjvzhk+ahrtbnpkvp6josbodhrwoi8vd3d3lmrvbmv3cy5uzxqvbgl6b25nym8gpc9i
b2r5pjxodg1spg==
——=_part_0_8568863.1110043370687–
3.启动javamail的调试模式,可以在发送和接收邮件的时候方便查看详细的调试信息,只需要:
//session session = session.getinstance(system.getproperties());
session.getproperties().setproperty("mail.debug","true");
session.setdebug(true);
//默认的是输出调试信息到控制台
4.显式连接到需要验证的smtp服务器进行邮件发送。
一般的资料都说是自己继承javax.mail.authenticator.
代码如下:
package com.lizongbo.javamail;
import javax.mail.*;
public class mailauthenticator extends authenticator {
public mailauthenticator() {
}
private string muser;
private string mpass;
public mailauthenticator(string username, string password) {
super();
muser = username;
mpass = password;
}
public passwordauthentication getpasswordauthentication() {
return new passwordauthentication(muser, mpass);
}
}
调用的示例:
session session = session.getinstance(system.getproperties(),new mailauthenticator ("lizongbo","password") );
mimemessage msg = new mimemessage(session);
transport.send(msg);
其实也可以不继承这个class,而是直接使用用户名和密码连接到邮件服务器进行邮件发送操作。
mimemessage msg = new mimemessage( (session)null);
msg.setfrom(new internetaddress("lizongbo@gmail.com"));
msg.setrecipient(message.recipienttype.to,
new internetaddress("lizongbo@msn.com"));
//中间代码略去
msg.savechanges();
transport smtptransport = session.gettransport("smtp");
smtptransport.connect("smtp.163.com", 25, "lizongbo", "password");
//切忌不可以使用 smtptransport.send(msg)和smtptransport.send(msg,msg.getallrecipients());
//因为这两个是静态方法,无法获取与smtptransport这个实例有关的参数.
smtptransport.sendmessage(msg, msg.getallrecipients());
具体可以通过阅读javamail1.3.2的源代码来发现一些细节差异。
