You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot邮件转发接口开发遇连接及Mercury配置错误求助

问题分析与解决方案

第一个错误:连接localhost:25被拒绝

错误提示显示Spring Mail尝试连接localhost:25,说明你的Gmail SMTP配置未被正确加载,系统 fallback到了默认的本地SMTP服务器。

解决步骤:

  1. 修正依赖与注入冲突
    你的EmailService同时使用@AllArgsConstructor和@Autowired会导致注入逻辑混乱,保留一种注入方式即可:

    @Service
    @AllArgsConstructor
    public class EmailService {
        // 去掉@Autowired,@AllArgsConstructor会自动注入JavaMailSender
        private JavaMailSender emailSender;
    
        public String sendSimpleMessage(String from, String to, String subject, String text) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
            emailSender.send(message);
    
            return "Thank you for reporting your problem. We'll get back to you soon";
        }
    }
    
  2. 验证配置加载

    • 确保application.properties放在src/main/resources目录下,Spring Boot会自动加载该文件。
    • 启动应用时查看日志,搜索spring.mail.host,确认实际加载的值是smtp.gmail.com而非localhost。
    • 检查配置键名是否有拼写错误,比如spring.mail.host不要写成spring.mail.hosts之类的。
  3. 排除配置覆盖
    检查是否存在其他配置文件(如application-dev.properties)或环境变量覆盖了spring.mail.*相关配置。


第二个错误:554 Shunned connection

这个错误是SMTP服务器(Gmail)拒绝了连接,通常和认证、安全设置或中继配置有关。

解决步骤:

  1. 直接使用Gmail SMTP,跳过Mercury中继
    既然你已经配置了Gmail的SMTP参数,无需通过Mercury中继,直接让Spring Mail连接Gmail服务器即可:

    • 关闭Mercury服务,避免本地SMTP服务器干扰。
    • 确认Gmail账号安全设置:
      • 开启两步验证,生成专用的应用密码(App Password)替换配置中的MyAppPassword。
      • 检查Gmail账号登录活动,若有异常登录记录,需手动解锁。
  2. 修正发件人地址
    Gmail SMTP不允许随意设置发件人地址,message.setFrom()的值必须与配置中的spring.mail.username一致。修改代码:

    @Service
    @AllArgsConstructor
    public class EmailService {
        private JavaMailSender emailSender;
        @Value("${spring.mail.username}")
        private String senderEmail;
    
        public String sendSimpleMessage(String to, String subject, String text) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(senderEmail); // 使用配置的邮箱作为发件人
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
            emailSender.send(message);
    
            return "Thank you for reporting your problem. We'll get back to you soon";
        }
    }
    

    对应的EmailController调整参数:

    @PostMapping("/support")
    public String sendEmail(@RequestBody EmailDTO email) {
        return emailService.sendSimpleMessage(
                "example@gmail.com", // 收件人邮箱
                email.getSubject(),
                email.getMessage());
    }
    
  3. 若坚持使用Mercury中继

    • 确保Mercury的SMTP中继配置正确:
      • 目标服务器设为smtp.gmail.com,端口587。
      • 在Mercury中配置Gmail的用户名和应用密码作为认证信息。
      • 开启TLS加密,匹配Gmail的要求。
    • 查看Mercury日志,排查具体的连接或认证错误。

内容的提问来源于stack exchange,提问作者Gh0st

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 03:12:13