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

JavaMail发送GMX邮件抛出500 Syntax error命令未识别异常

问题根因

核心是SMTP加密连接配置冲突,叠加JavaMail Session缓存问题,导致同一份代码在不同设备表现不一致:

  1. 你混用了GMX两套互不兼容的SMTP端口规则:
    • 465端口为隐式SSL端口,连接建立后直接走TLS加密通道,不识别明文SMTP指令
    • 587端口为STARTTLS端口,初始建立明文连接,通过STARTTLS指令升级为加密连接
  2. 现有代码同时配置了mail.smtp.socketFactory.class强制走SSLSocketFactory连接465,又设置mail.smtp.port=587并开启STARTTLS,直接导致JavaMail连接逻辑混乱:从调试日志可以看到,程序先尝试连587明文端口,又异常切到465端口,在需要SSL握手的465端口直接发送明文EHLO/HELO指令,被服务端返回500语法错误。
  3. 你使用Session.getDefaultInstance()获取邮件会话,该方法会复用JVM中已缓存的默认Session配置,如果本地之前运行过其他JavaMail相关程序,传入的新配置会被直接忽略;同时你用System.getProperties()存储配置,既会污染JVM全局参数,也可能被系统中已有的SMTP配置覆盖,这是跨设备运行结果不一致的核心诱因。
修复方案

二选一即可,优先推荐587端口的STARTTLS模式,兼容性更好。

方案1:587端口 + STARTTLS(推荐)

删除所有465端口、SSL Socket Factory相关配置,替换Session获取方法,修正后的完整方法代码如下:

private static void btnSendEmail(ActionEvent e) throws MessagingException, Exception {
    
    final String user = "...@gmx.at";
    final String password = "***********";
    final String smtpHost = "mail.gmx.net";
    final String smtpPort = "587";

    // 新建空Properties对象,不要用System.getProperties()避免配置干扰
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.host", smtpHost);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", smtpPort);
    props.put("mail.smtp.starttls.enable", "true");
    // 强制要求STARTTLS,防止明文连接降级
    props.put("mail.smtp.starttls.required", "true");

    Authenticator auth = new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password);
        }
    };

    // 用getInstance替代getDefaultInstance,每次生成新Session不使用缓存
    Session mailSession = Session.getInstance(props, auth);
    mailSession.setDebug(true);

    String sender = "...@gmx.at";
    String receiver = JOptionPane.showInputDialog("Enter the receiver of the message");
    String subject = "Subject";
    String message = "text";

    MimeMessage msg = new MimeMessage(mailSession);
    msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
    msg.addHeader("format", "flowed");
    msg.addHeader("Content-Transfer-Encoding", "8bit");

    msg.setFrom(new InternetAddress(sender));
    msg.setReplyTo(InternetAddress.parse(sender, false));
    msg.setSubject(subject, "UTF-8");
    msg.setSentDate(new Date());
    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiver, false));

    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(message, "UTF-8");

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    msg.setContent(multipart);

    System.out.println("Sending ...");
    Transport.send(msg);
    System.out.println("Done");
}

方案2:465端口 + 隐式SSL

如果要使用465隐式SSL端口,删除STARTTLS相关配置,统一端口参数即可,核心配置段如下:

Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// 不需要配置mail.smtp.starttls相关参数
排错补充

如果调整配置后仍无法连接,逐一检查以下项:

  • 确认GMX邮箱后台已开启SMTP服务,新注册账号部分需要手动开启第三方客户端访问权限,部分账号需要生成专用的第三方客户端登录密码,不能直接使用网页登录密码
  • 检查本地防火墙、杀毒软件是否拦截了587/465端口的出站连接,是否开启了网络代理篡改SSL握手流量
  • 调试阶段保持mailSession.setDebug(true)开启,观察日志中连接的端口、握手流程是否符合预期

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 09:18:18