第一步:注册邮箱
配置客户端授权密码。如图:
勾选POP3/SMTP服务、IMAP/SMTP服务。如图:
第二步:jar包准备
<!-- 发送邮件依赖 -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
第三步:创建配置文件 mailConfig.properties
在resouces文件夹下创建mailConfig.properties配置文件:
mailConfig.properties
#服务器
mailHost=smtp.163.com
#端口号
mailPort=25
#邮箱账号
mailUsername=**********@163.com
#邮箱授权码
mailPassword=*******
#时间延迟
mailTimeout=25000
#发送人
mailFrom=**********@163.com
第四步:获得资源文件内容
package com.bigname.common.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 邮件配置工具
*
* @author zeyu
* 2019-06-09
*/
public class MailConfig {
private static final String PROPERTIES_DEFAULT = "mailConfig.properties";
public static String host;
public static Integer port;
public static String userName;
public static String passWord;
public static String emailForm;
public static String timeout;
public static String personal;
public static Properties properties;
static{
init();
}
/**
* 初始化
*/
private static void init() {
properties = new Properties();
try{
InputStream inputStream = MailConfig.class.getClassLoader().getResourceAsStream(PROPERTIES_DEFAULT);
properties.load(inputStream);
inputStream.close();
host = properties.getProperty("mailHost");
port = Integer.parseInt(properties.getProperty("mailPort"));
userName = properties.getProperty("mailUsername");
passWord = properties.getProperty("mailPassword");
emailForm = properties.getProperty("mailFrom");
timeout = properties.getProperty("mailTimeout");
personal = "泽禹";
} catch(IOException e){
e.printStackTrace();
}
}
}
第五步:发送邮件工具类
package com.bigname.common.utils;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
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;
/**
* 邮件发送工具
*
* @author zeyu
* 2019-06-09
*/
public class MailUtil {
private static final String HOST = MailConfig.host;
private static final Integer PORT = MailConfig.port;
private static final String USERNAME = MailConfig.userName;
private static final String PASSWORD = MailConfig.passWord;
private static final String emailForm = MailConfig.emailForm;
private static final String timeout = MailConfig.timeout;
private static final String personal = MailConfig.personal;
private static Session mailSender = createMailSender();
/**
* 邮件发送器
*
* @return 配置好的工具
*/
private static Session createMailSender() {
Properties p = new Properties();
p.setProperty("mail.transport.protocol", "SMTP");
p.setProperty("mail.host", "smtp.163.com");
/*p.setProperty("mail.smtp.timeout", timeout);*/
p.setProperty("mail.smtp.auth", "true");
// 创建验证器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);//注意此处password为smtp验证码,不是密码
}
};
Session session = Session.getInstance(p, auth);
return session;
}
/**
* 发送邮件
*
* @param to 接受人
* @param subject 主题
* @param html 发送内容
* @throws MessagingException 异常
* @throws UnsupportedEncodingException 异常
*/
public static void sendMail(String to, String subject, String html) throws MessagingException,UnsupportedEncodingException {
Message message = new MimeMessage(mailSender);
message.setFrom(new InternetAddress(USERNAME)); // 设置发送者
message.setRecipient(RecipientType.TO, new InternetAddress(to)); // 设置发送方式与接收者
message.setSubject(subject);
message.setContent(html, "text/html;charset=utf-8");
// 获得Transport实例对象
Transport transport = mailSender.getTransport("smtp");
// 打开连接
transport.connect(HOST, USERNAME, PASSWORD);
// 将message对象传递给transport对象,将邮件发送出去
transport.sendMessage(message, message.getAllRecipients());
// 关闭连接
transport.close();
}
}
第六步:在业务控制层controller添加调用方法
@RequestMapping(value = "/registerMail", method = RequestMethod.POST)
@ResponseBody
public String registerMail(String email){
System.out.println("==进入发送邮件方法了=="+email);
String subject = "注册验证";
String ver = StringUtils.getVerification(6);
String html = "您好!您的验证码为:<a>"+ver+"</a>";
//调用邮件发送方法
try {
MailUtil.sendMail(email, subject, html);
return ver;
} catch (Exception e) {
e.printStackTrace();
return "no";
}
}
第七步:jsp页面调用方法
function getVer(){//调用后台发送邮件方法
var email = $('#email').val();
if(email == ""){
alert("邮箱是空的!");
}else{
$.ajax({
type : "POST",
data: {"email":email},
url: "${ctx}/registerMail",
dataType : 'text',
success: function(date){
if(date == "no"){
alert("发送失败!");
}else{
$('#ver').val(date);
alert("发送成功!");
}
}
});
}
}
<input type="button" value="获取验证码" class="button" style="width:100px;" onclick="getVer()" />