maven依赖
<!-- 发送邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
邮件服务与实现
@Component
@Slf4j
public class EmailUtil {
@Resource
private JavaMailSender jms;
@Value("${spring.mail.username}")
public String senderEmail;
public boolean sendHtmlMessage(String receiver, String subject, String content) {
try {
MimeMessage mimeMessage = jms.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(senderEmail);
mimeMessageHelper.setTo(receiver);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content,true);
jms.send(mimeMessage);
} catch (Exception e) {
log.error("发送邮件失败:{}",e.getMessage());
return false;
}
return true;
}
}
spring:
mail:
host: smtp.exmail.qq.com
port: 587
username: *****
password: *****
properties:
mail:
smtp:
auth: true
starttls:
ennable: false
required: false