文章目录
注意:Java邮件发送需要开启SMTP服务。
1、导入相关依赖
<properties>
<spring.version>5.2.3.RELEASE</spring.version>
</properties>
<!-- 邮件发送支持包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 邮件发送 -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
2、邮件发送类
package com.hx.skyDrive.util;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
@Component
public class SendMailUtil {
private String sendEmail; //发件人邮箱
private String pwd; //发件箱授权码
private String host;
public void setSendEmail(String sendEmail) {
this.sendEmail = sendEmail;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public void setHost(String host) {
this.host = host;
}
/**
* @param sendEmail 邮件发送账号
* @param pwd 邮件SMTP授权码
* @param host 邮件主机
*/
public SendMailUtil(String sendEmail, String pwd, String host) {
super();
this.sendEmail = sendEmail;
this.pwd = pwd;
this.host = host;
}
public SendMailUtil() {
super();
}
/**
* 邮件发送类
* @param receiveMail 接收方邮箱
* @param mainText 发送的正文
* @param subject 发送的主题
* @return
*/
public boolean sendEmail(String receiveMail,String mainText,String subject){
try {
if(StringUtil.checkNull(receiveMail,mainText,subject)){
throw new Exception("参数不可为空,请检查参数:receiveMail,mainText,subject");
}
if(StringUtil.checkNull(sendEmail,pwd,host)){
throw new Exception("请检查参数配置:sendEmail,pwd,host");
}
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
senderImpl.setHost(host); //邮箱主机。若是网易的则使用smtp.163.com,QQ:smtp.qq.com
senderImpl.setDefaultEncoding("UTF-8"); //编码集
//建立邮件的消息,我们需要发送的是html格式的邮件
MimeMessage mimeMessage = senderImpl.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
//设置收件人,寄件人,邮件主题
messageHelper.setTo(receiveMail);
messageHelper.setFrom(sendEmail);
messageHelper.setSubject(subject);
//设置邮件正文
messageHelper.setText(mainText,true);
//设置名称
senderImpl.setUsername(sendEmail); //发件箱
senderImpl.setPassword(pwd); //发件箱密码
Properties prop = new Properties();
prop.put("mail.smtp.auth", "true"); //让服务器去认证用户名何密码
prop.put("mail.smtp.timeout", "2500"); //连接超时时间
senderImpl.setJavaMailProperties(prop);
senderImpl.send(mimeMessage);
return true;
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
3.1、测试一:MVC容器管理的调用方法
3.1. 在spring-beans.xml中进行邮件发送的配置
<!-- 邮件发送配置 -->
<bean id="sendMailUtil" class="com.hx.skyDrive.util.SendMailUtil">
<!-- 配置邮件地址,即你的发送账号 -->
<property name="sendEmail" value="xxx@xx.com"></property>
<!-- 配置SMTP授权码,需要开启SMTP服务 -->
<property name="pwd" value="SMTP授权码xxxx"></property>
<!-- 配置邮件SMTP主机 若是QQ邮箱:smtp.qq.com-->
<property name="host" value="smtp.163.com"></property>
</bean>
3.2 开始调用
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private SendMailUtil sendMail;
@RequestMapping("/code")
public int sendCode(String email,HttpSession session){
if(StringUtil.checkNull(email)){
return -2; //邮箱为空
}
int code = (int)((Math.random()*9+1)*100000);
String mainText = "<!DOCTYPE><html><h1>您的验证码为: <font color='red'><b>"+code+"</b></font></h1></html>";
boolean result = sendMail.sendEmail(email, "尊敬的用户", mainText, "HUATHY SKY DRIVE");
if(result){
return 1;
}
return -1;
}
}
3.3 前端html 通过 user/code 访问
$.post("user/code",{email:email},function(data){
console.log(data);
data = parseInt($.trim(data));
if(data == 1){
layer.msg('验证码发送成功,请前往邮箱查看...');
}else{
layer.msg('验证码发送失败,请稍后重试...');
});
4、测试二:通过实例化调用
public class Test {
public static void main(String[] args) {
SendMailUtil sendMail = new SendMailUtil("发送邮箱账号@163.com", "邮件授权码", "smtp.163.com");
boolean result = sendMail.sendEmail("接受邮箱@qq.com", "HUATHY", "您好!测试邮件!勿回!", "测试!");
if(result){
System.out.println("邮件发送成功...");
}else{
System.out.println("邮件发送失败...");
}
}
}