jdk:17
springboot:3.0+
POM依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.0.2</version>
</dependency>
工具类
/**
* 邮件发送消息
*/
@UtilityClass
public class MailUtil {
/**
* 邮件发送消息
*
* @param host SMTP服务器
* @param port SMTP端口
* @param username SMTP用户名
* @param password SMTP密码
* @param from 发件人
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
public Boolean sendNotice(String host, int port, String username, String password, String from, String protocol, String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject(subject);
message.setText(content);
message.setTo(to);
message.setFrom(from);
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setUsername(username);
mailSender.setPassword(password);
mailSender.setProtocol(protocol);
mailSender.setDefaultEncoding(CommonConstants.UTF8);
Properties properties = new Properties();
Map<String, String> source = new HashMap<>();
source.put("mail.smtp.auth", "true");
source.put("mail.smtp.socketFactory.port", String.valueOf(port));
source.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
source.put("mail.smtp.socketFactory.fallback", "false");
source.put("mail.smtp.starttls.enable", "true");
source.put("mail.smtp.starttls.required", "true");
properties.putAll(source);
mailSender.setJavaMailProperties(properties);
mailSender.send(message);
return Boolean.TRUE;
}
}
标签:mailSender,SpringBoot,param,发送,source,mail,message,邮件,String
From: https://www.cnblogs.com/leepandar/p/18207871