工具类:MailConfig.java和MailUtil .java
1.MailConfig.java
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 ="XXXXXXX";
public static Integer port=465;
public static String userName="XXXXXXX";
public static String passWord="XXXXXXX";
public static String emailForm="XXXXXXX";
public static String timeout="2500";
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,"UTF-8"));
// 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();
// }
// }
}
2.MailUtil .java
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.ruoyi.common.config.MailConfig;
import com.ruoyi.common.utils.AsyncUtil;
import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
/**
* 阿里云禁止25端口发送邮件
*
* @author leng
*/
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 subject, String html)
throws MessagingException, UnsupportedEncodingException {
AsyncUtil.run(()->{
try {
// 1. 创建参数配置, 用于连接邮件服务器的参数配置
// 参数配置
Properties props = new Properties();
// 使用的协议(JavaMail规范要求)
props.setProperty("mail.transport.protocol", "smtp");
// 发件人的邮箱的 SMTP 服务器地址
props.setProperty("mail.smtp.host", HOST);
// 需要请求认证
props.setProperty("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
// SSLSocketFactory类的端口
props.put("mail.smtp.socketFactory.port", PORT);
// SSLSocketFactory类
props.put("mail.smtp.ssl.socketFactory", new MailSSLSocketFactory());
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
// 网易提供的ssl加密端口,QQ邮箱也是该端口
props.put("mail.smtp.port", PORT);
// 2. 根据配置创建会话对象, 用于和邮件服务器交互
Session session = Session.getDefaultInstance(props);
// 设置为debug模式, 可以查看详细的发送 log
session.setDebug(true);
// 1. 创建一封邮件
MimeMessage message = new MimeMessage(session);
// 2. From: 发件人
message.setFrom(new InternetAddress(USERNAME, personal, "UTF-8"));
// 3. To: 收件人(可以增加多个收件人、抄送、密送)
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to, "个人网站", "UTF-8"));
// 4. Subject: 邮件主题
message.setSubject(subject, "UTF-8");
// 5. Content: 邮件正文(可以使用html标签)
message.setContent(html, "text/html;charset=UTF-8");
// 6. 设置发件时间
message.setSentDate(new Date());
// 7. 保存设置
message.saveChanges();
// 4. 根据Session获取邮件传输对象
Transport transport = session.getTransport();
// 5. 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错
transport.connect(USERNAME, PASSWORD);
// 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人,
// 密送人
transport.sendMessage(message, message.getAllRecipients());
// 7. 关闭连接
transport.close();
}catch (Exception e){
System.out.println("邮件推送异常" + to + subject);
System.out.println("邮件推送异常:"+e.toString());
}
});
}
}