依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
The Spring Framework provides an abstraction for sending email by using the JavaMailSender interface, and Spring Boot provides auto-configuration for it as well as a starter module.
🔝See the reference documentation for a detailed explanation of how you can use JavaMailSender.
SimpleMailMessage
public class SimpleMailMessage implements MailMessage, Serializable {
@Nullable
private String from;
@Nullable
private String replyTo;
@Nullable
private String[] to;
//抄送
@Nullable
private String[] cc;
//密送
@Nullable
private String[] bcc;
@Nullable
private Date sentDate;
@Nullable
private String subject;
@Nullable
private String text;
spring:
mail:
host: smtp.163.com #SMTP服务器地址
username: 1524 #登陆账号
password: HIOIT #登陆密码(或授权码)
properties:
# mail.smtp.auth: true
# mail.smtp.starttls.enable: true
mail.smtp.connectiontimeout: 5000
mail.smtp.timeout: 3000
mail.smtp.writetimeout: 5000
servlet:
multipart:
max-file-size: 10MB #限制单个文件大小
max-request-size: 50MB #限制请求总量
@Service
public class EmailService {
@Resource
private JavaMailSender mailSender;
public void sendEmail(String to, String subject, String text) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(text);
message.setCc();
mailSender.send(message);
}
}