话不多说直接上代码:
MailUtil类(核心)
MailUtil.java
package com.myproject.aiplatform.aiopenplatform.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
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 final String subject = MailConfig.subject;
private static final String html = MailConfig.html;
private static JavaMailSenderImpl mailSender = createMailSender();
/**
* 邮件发送器
*
* @return 配置好的工具
*/
private static JavaMailSenderImpl createMailSender() {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost(HOST);
sender.setPort(PORT);
sender.setUsername(USERNAME);
sender.setPassword(PASSWORD);
sender.setDefaultEncoding("Utf-8");
Properties p = new Properties();
p.setProperty("mail.smtp.timeout", timeout);
p.setProperty("mail.smtp.auth", "false");
sender.setJavaMailProperties(p);
return sender;
}
/**
* 发送邮件
*
* @param to 接受人
* @param subject 主题
* @param html 发送内容
* @throws MessagingException 异常
* @throws UnsupportedEncodingException 异常
*/
public static void sendMail(String to, String html) throws MessagingException,UnsupportedEncodingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
// 设置utf-8或GBK编码,否则邮件会有乱码
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
messageHelper.setFrom(emailForm, personal);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(html, true);
// messageHelper.addAttachment("", new File(""));//附件
mailSender.send(mimeMessage);
}
}
MailConfig类(获取.properties文件内容)
package com.myproject.aiplatform.aiopenplatform.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
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 String html;
public static String subject;
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();
//解决中文乱码,采取reader把inputStream转换成reader用字符流来读取中文
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
properties.load(bf);
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 = properties.getProperty("personal");
html = properties.getProperty("html");
subject = properties.getProperty("subject");
} catch(IOException e){
e.printStackTrace();
}
}
}
mailConfig.properties(配置一些邮件主题、发件人等信息)
#服务器
mailHost=smtp.163.com
#端口号
mailPort=25
#邮箱账号
mailUsername=***@163.com
#邮箱授权码
mailPassword=***
#时间延迟
mailTimeout=25000
#发送人
mailFrom=***@163.com
#发件人
personal=黑白色调
#主题
subject=***
#内容模板
html=您的邮箱验证码为:
PS:1、必须登录163邮箱授权第三方使用(也就是得到邮箱授权码)。
2、.properties文件中邮箱账号和发送人必须一致。
邮箱授权码获取(登录邮箱—设置—客户端授权密码—开启):