百度找了各种使用javaMail发送邮件,大多数是使用163邮箱,不需要ssl加密,但使用qq邮箱的时候需要使用.这里将我个人使用经验分享给大家也算是我的个人笔记了.
好了,这里正式开始!!!!
首先,我的使用背景是javaweb项目,使用的是spring框架(spring框架封装了javaMail,这里没有使用,感兴趣的可以自己百度,后续可能我会使用)
第一步:开通qq邮箱POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
进入qq邮箱 设置->账户->下翻找到
POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务->开通
不开通的话,在使用的时候会报错,我这里报错显示的是乱码,大概的意思就是需要使用开通服务后获取的授权码登陆
第二步:编写代码
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
/**
* @author hewe
* @date : 2016年9月19日 下午11:11:12
*
* 用于web程序发送和接受邮件 这里使用的事qq邮箱 需要的包 mail.jar 此类封装了发送接受邮件的相关成员
*/
public class HeweMail {
// 发件人
private final static String SEND_NAME = "*******@qq.com";
// 发件人密码,这里使用的事授权密码
private final static String SEND_PASS = "***********";
// 属性
private Properties props = null;
private MailSSLSocketFactory msf = null;//用于ssl
private Session session = null;
private Message msg = null;
private Transport transport = null;
public HeweMail() {
props = new Properties();
// debug调试
props.setProperty("mail.debug", "true");
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件服务器主机名
props.setProperty("mail.host", "smtp.qq.com");
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");
}
/*
* MailSSLSocketFactory初始化
*/
private Boolean mailSSLConfig() {
try {
msf = new MailSSLSocketFactory();
msf.setTrustAllHosts(true);
} catch (GeneralSecurityException e) {
e.printStackTrace();
return false;
}
return true;
}
/*
* prop初始化
*/
private Boolean propConfig() {
if (mailSSLConfig()) {
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", msf);
return true;
}
return false;
}
/*
* one to one
*/
public Boolean sendOneToOne(String subJect, String content, String to) {
if (!propConfig()) {
return false;
}
session = Session.getInstance(props);
msg = new MimeMessage(session);
try {
msg.setSubject(subJect);
msg.setText(content);
msg.setFrom(new InternetAddress(SEND_NAME));
transport = session.getTransport();
transport.connect("smtp.qq.com", SEND_NAME, SEND_PASS);
transport.sendMessage(msg,
new Address[] { new InternetAddress(to) });
transport.close();
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
}
这里是 我封装的一个类,没有完善,多多包涵.
介绍下这个类
两个常量SEND_NAME,SEND_PASS用来放你用来发送邮件的用户名和密码,密码使用开通服务后的授权码
构造函数中初始化prop属性
用
mailSSLConfig()初始化ssl
<span style="font-family: 'lucida Grande', Verdana, 'Microsoft YaHei'; font-size: 14px;"></span><pre name="code" class="java">propConfig()添加初始化后的ssl到prop中,这样就添加了ssl认证
<span style="font-family: 'lucida Grande', Verdana, 'Microsoft YaHei'; font-size: 14px;"></span><pre name="code" class="java">sendOneToOne()这个方法则是真正发送邮件的方法,这里是一个邮件发送给一个收件人,三个参数分别是邮件标题,内容和收件人地址
使用时只需new一个此类对象,调用这个方法就可以
最后,如果报错提示ClassNotFound:com.sun.mail.<strong style="font-family: 'lucida Grande', Verdana, 'Microsoft YaHei';"></strong><pre name="code" class="java" style="display: inline !important;">MailSSLSocketFactory类似这个的缺少一个javamail-1.4.5包,可以到官网下载,导入项目中即可
<strong style="font-family: 'lucida Grande', Verdana, 'Microsoft YaHei';"></strong><pre name="code" class="java" style="display: inline !important;">