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("
[email protected]"));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress("
[email protected]"));
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:
[email protected]
To:
[email protected]
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("
[email protected]"));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress("
[email protected]"));
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:
[email protected]
To:
[email protected]
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("
[email protected]"));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress("
[email protected]"));
//中间代码略去
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的源代码来发现一些细节差异。