1. 首先构建一个继承自javax.mail.Authenticator的具体类,重写里面的getPasswordAuthentication()方法。
/**
* 邮箱认证信息
* @author Leon
*
*/
public class MyAuthenticator extends Authenticator {
private String userName;
private String password;
public MyAuthenticator(){
}
public MyAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2. 构建一个邮件pojo,注:提供get()/set()和toString()方法
/**
* 邮件类
* @author Leon
*
*/
public class SimpleMail implements Serializable{
/** 单发收件人邮箱 */
private String recipient;
/** 一大波收件人邮箱(若该属性有值,则采用群发) */
private List<String> recipientList;
/** 邮件主题 */
private String subject;
/** 邮件内容 */
private Object content;
}
3.
构建一个properties文件,该文件中存放SMTP服务器地址等参数。以及构造邮件内容等......
mail.username=111111111@qq.com
mail.pwd=ffjwfvknqw
mail.host=smtp.qq.com
mail.ssl=true
#email template
email.subject = Retrieve account password.
email.header = <div>Hi, Dear user\:</div> <div> we have already received your password request for password retrieve\: </div>
email.footer = <div>Your Login password is %s.</div>
4. 接下来是怎样加载配置文件了,这里我是用的是Spring来加载
<bean class="com.xuexi.utils.Config">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:/retrievePassword/*.properties</value>
</list>
</property>
</bean>
<bean id="simpleMail" class="com.xuexi.utils.mail.SimpleMailSender">
<constructor-arg index="0" value="${mail.username}"/>
<constructor-arg index="1" value="${mail.pwd}"/>
<constructor-arg index="2" value="${mail.host}"/>
<constructor-arg index="3" value="${mail.ssl}"/>
</bean>
5.
构建好了邮件内容,并指定发送人,收信人,主题,内容等等。使用javax.mail.Transport工具类发送邮件。
import java.util.List;
import java.util.Properties;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* 邮件发送类
* @author Leon
*
*/
public class SimpleMailSender {
private Properties props;
private MyAuthenticator auth;
private Session session;
/**
* 发送邮件,若mail参数设置了recipientList属性,则采用群发,否则单发
* @param mail
* @throws AddressException
* @throws MessagingException
*/
public void send(SimpleMail mail) throws AddressException, MessagingException{
List<String> recipients = mail.getRecipientList();
if(recipients != null && recipients.size() > 0){
send(recipients, mail.getSubject(), mail.getContent());
} else {
send(mail.getRecipient(), mail.getSubject(), mail.getContent());
}
}
public SimpleMailSender(){
}
/**
*
* @param userName 登录用户名
* @param password 登录密码
* @param hostName 邮箱服务名
*/
public SimpleMailSender(String userName, String password, String hostName){
init(userName, password, hostName);
}
public SimpleMailSender(String userName, String password){
// test@163.com -> hostName=smtp.163.com
String hostName = "smtp." + userName.split("@")[1];
init(userName, password, hostName);
}
private void init(String userName, String password, String hostName){
auth = new MyAuthenticator(userName, password);
props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", hostName);
session = Session.getInstance(props, auth);
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
public MyAuthenticator getAuth() {
return auth;
}
public void setAuth(MyAuthenticator auth) {
this.auth = auth;
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
/**
* 单发
* @param recipient 收件人邮箱
* @param subject 主题
* @param content 内容
* @throws MessagingException
* @throws AddressException
*/
private void send(String recipient, String subject, Object content) throws AddressException, MessagingException{
// 基于该session创建一封Mime邮件
MimeMessage msg = new MimeMessage(session);
// 设置发件人信息
msg.setFrom(new InternetAddress(auth.getUserName()));
// 设置收件人信息
msg.setRecipient(RecipientType.TO, new InternetAddress(recipient));
// 设置主题
msg.setSubject(subject);
// 设置内容及编码方式
msg.setContent(content.toString(), "text/html;charset=utf-8");
// 发送
Transport.send(msg);
}
/**
* 群发
* @param recipients 一大波收件人
* @param subject 邮件主题
* @param content 邮件内容
* @throws AddressException
* @throws MessagingException
*/
private void send(List<String> recipients, String subject, Object content) throws AddressException, MessagingException{
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(auth.getUserName()));
// 一大波收件人的地址
InternetAddress[] addrs = new InternetAddress[recipients.size()];
for (int i = 0; i < addrs.length; i++) {
addrs[i].setAddress(recipients.get(i));
}
// 设置收件人信息
msg.setRecipients(RecipientType.TO, addrs);
msg.setSubject(subject);
msg.setContent(content.toString(), "text/html;charset=utf-8");
Transport.send(msg);
}
}
6.测试结果