Avast杀毒软件导致Spring Boot邮件发送失败问题求助
问题分析
关闭Avast杀毒软件时Spring Boot应用可正常发送邮件,开启则触发MailSendException,核心错误是SMTP连接被拦截(错误码10060),这是因为Avast的邮件防护模块对587端口的STARTTLS连接做了拦截。以下是无需依赖用户修改本地杀毒软件设置的通用解决方案:
方案1:切换到Gmail的SSL加密端口(465)
Avast对STARTTLS(587端口,先明文后加密)的拦截概率更高,改用全程SSL加密的465端口可绕过该拦截。修改配置文件如下:
spring.mail.host=smtp.gmail.com spring.mail.port=465 spring.mail.username=*my-username* spring.mail.password=*my-generated-password* spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com
465端口从连接建立之初就使用SSL加密传输,避开了杀毒软件对明文阶段的检测规则。
方案2:添加连接超时与重试机制
配置超时参数减少连接等待时间,同时结合重试机制应对临时拦截:
1. 添加超时配置
spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=5000 spring.mail.properties.mail.smtp.writetimeout=5000
2. 启用Spring Retry重试
先添加Maven依赖:
<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
然后在邮件发送方法上添加重试注解:
@Retryable(value = {MessagingException.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000)) public void sendEmail(String to, String subject, String content) { // 你的邮件发送逻辑 } @Recover public void handleSendFailure(MessagingException e) { // 重试失败后的降级处理,比如记录日志、通知管理员 }
方案3:改用第三方邮件中转服务
如果上述方法无效,可使用SendGrid、Mailgun等第三方邮件服务,它们的SMTP配置更不容易被杀毒软件拦截,且稳定性更强。以SendGrid为例,配置如下:
spring.mail.host=smtp.sendgrid.net spring.mail.port=587 spring.mail.username=apikey spring.mail.password=*your-sendgrid-api-key* spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true
额外注意事项
- 确保使用Gmail的应用专用密码(开启2FA时必须),不要使用账号原密码,这是Gmail SMTP发送的必要前提。
- 可在应用启动时添加SMTP连接测试逻辑,提前检测连接状态并给出用户提示。
内容的提问来源于stack exchange,提问作者Developer
相关产品推荐
相关产品推荐

