SpringBoot发送邮件Email


使用spring-boot-starter-mail发送邮件,使用SSL端口465

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-mail</artifactId>
	<version>2.0.6.RELEASE</version>
</dependency>
// 直接注入JavaMailSender 
@Autowired
private JavaMailSender javaMailSender;		


MimeMessage mimeMailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
mimeMessageHelper.setFrom("emailSender@xxx.com");//发送者
mimeMessageHelper.setTo("emailReceiver@xxx.com");//接收这
mimeMessageHelper.setSubject("邮件主题");
mimeMessageHelper.setText("邮件内容");
javaMailSender.send(mimeMailMessage);


application.properties

# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.xxx.com
# 默认端口为25,但一些云服务器都关闭了此端口,只能使用SSL端口465
spring.mail.port=465
spring.mail.protocol=smtp
spring.mail.username=service@xxx.com
#密码或授权码
spring.mail.password=密码或授权码
spring.mail.properties.mail.smtp.auth=true
# 开启SSL
spring.mail.properties.mail.smtp.ssl.enable=true
# QQ邮箱会使用到starttls
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.connectiontimeout=60000
spring.mail.properties.mail.smtp.timeout=60000
spring.mail.properties.mail.smtp.writetimeout=60000


# SMTP默认端口为25,但一些云服务器都关闭了此端口,只能使用SSL端口465
spring.mail.port=465
# 开启SSL
spring.mail.properties.mail.smtp.ssl.enable=true



email springboot

2020.11.18 00:01

https://www.meihaocloud.com.com/398.html , 欢迎转载,请在文章页标出原文连接 !


Copyright © 2020 千夕网 联系站长

粤公网安备 44030302001408号 粤ICP备19099833号-1