【springboot】发送邮箱不能够连接到主机

最近在导出邮箱的时候发现服务器报错,但是在本地就没事,提示错误

 nested exception is com.sun.mail.util.MailConnectException: 
 Couldn't connect to host, port: smtp.qq.com, 25; timeout -1;
  nested exception is:
	java.net.ConnectException: Connection timed out
	 (Connection timed out). 
	 Failed messages: com.sun.mail.util.MailConnectException:
	  Couldn't connect to host, port: smtp.qq.com, 25; timeout -1;

发现是连接不到主机,百度问题发现是因为阿里云处于安全考虑,TCP 25 端口默认被封禁。
可以向阿里云申请解封,也可以改为ssl加密465端口发送

我这里是将端口改成了465
在yml文件中修改mail配置

spring:
	mail:
	    host: smtp.qq.com
	    userName: xxxx@qq.com
	    passWord: 认证指令
	    default-encoding: UTF-8
	    properties:
	      mail:
	        smtp:
	          auth: true
	          starttls:
	            enable: true
	            required: true
	    port: 465
	    protocol: smtps

我们加上一下两个就可以了

 port: 465
 protocol: smtps
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是使用SpringBoot发送邮件的示例代码,其中包括了如何发送带附件的邮件和如何开启TLS验证: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.mail.MailProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootApplication @EnableConfigurationProperties(MailProperties.class) public class MailApplication { @Autowired private JavaMailSender mailSender; @Autowired private MailProperties mailProperties; public static void main(String[] args) { SpringApplication.run(MailApplication.class, args); } public void sendMailWithAttachment() throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(mailProperties.getUsername()); helper.setTo("recipient@example.com"); helper.setSubject("Test email with attachment"); // 添加附件 FileSystemResource file = new FileSystemResource(new File("attachment.txt")); helper.addAttachment("attachment.txt", file); // 发送邮件 mailSender.send(message); } } ``` 在application.properties文件中添加以下配置: ``` spring.mail.username=xxxxxxx@outlook.com spring.mail.password=xxxxxxxxx spring.mail.port=587 spring.mail.host=smtp-mail.outlook.com spring.mail.properties.mail.smtp.starttls.required=true ``` 注意:在使用Outlook发送邮件时,需要开启TLS验证,否则会显示匿名用户无法通过验证。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值