一 邮件发送
maven直接在pom.xml引入
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
1 发送邮件第一步:设置发送邮件的邮箱
package com.apk.openUser.utils;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
public class MailUtil {
public Session getSession(String sender,String password){
Session session;
try {
Properties props = new Properties();
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件服务器主机名 ,我这里用的qq企业邮箱作为发件人的
props.setProperty("mail.host", "smtp.exmail.qq.com");
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");
// 设置环境信息
MyAuthenricator authenricator = new MyAuthenricator(sender,password);
session = Session.getInstance(props, authenricator);
} catch (Exception e) {
// TODO: handle exception
session = null;
}
return session;
}
/**
* 客户端程序自己实现Authenticator子类用于用户认证
*/
static class MyAuthenricator extends Authenticator{
String user=null;
String pass="";
public MyAuthenricator(String user,String pass){
this.user=user;
this.pass=pass;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,pass);
}
}
/**
* 收件人邮箱格式校验
* @param receivers
* @return
*/
public Boolean compileEmail(List<String> receivers){
String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern regex = Pattern.compile(check);
for(String receiver : receivers){
Matcher matcher = regex.matcher(receiver);
boolean isMatched = matcher.matches();
if(!isMatched) return false;
}
return true;
}
}
2 设置发送邮件的相关信息
package com.apk.openUser.utils;
import java.io.File;
import java.securit