最近项目需要添加一个发送邮件的功能,参考了网上了一个例子,代码如下:
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import com.sun.mail.util.MailSSLSocketFactory;
public class SendMailThread extends Thread {
private String mailAdr;
private String content;
private String subject;
public SendMailThread(String mailAdr, String subject, String content) {
// TODO Auto-generated constructor stub
super();
this.mailAdr = mailAdr;
this.subject = subject;
this.content = content;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
sendMail(mailAdr, subject, content);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendMail(String mailAdr, String subject, String content) throws Exception {
// TODO Auto-generated method stub
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
final Properties props = new Properties();
// 表示SMTP发送邮件,需要进行身份验证
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
// 端口
/* props.put("mail.smtp.port", 25); */
// smtp登陆的账号、密码 ;需开启smtp登陆
props.setProperty("mail.debug", "true");
props.put("mail.user", "发送者的QQ邮箱");
props.put("mail.password", "QQ邮箱授权码");
//特别需要注意,要将ssl协议设置为true,否则会报530错误
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
try {
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
// 设置收件人
InternetAddress to = new InternetAddress(mailAdr);
message.setRecipient(RecipientType.TO, to);
// 设置抄送
// InternetAddress cc = new InternetAddress("luo_aaaaa@yeah.net");
// message.setRecipient(RecipientType.CC, cc);
// 设置密送,其他的收件人不能看到密送的邮件地址
// InternetAddress bcc = new InternetAddress("aaaaa@163.com");
// message.setRecipient(RecipientType.CC, bcc);
// 设置邮件标题
message.setSubject(subject);
// 设置邮件的内容体
message.setContent(content, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SendMailThread d = new SendMailThread("接收者的邮箱", "测试邮件标题",
"测试邮件001 <br>内容....");
d.start();
System.out.println("程序继续...");
}
}
这里可以开启多个线程发送邮件,本例使用QQ邮箱测试,需要将QQ邮箱的设置里的账户SMTP服务打开。需要导入mail.jar包。