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

Java Web应用发邮件遇javax.mail.AuthenticationFailedException求助

解决JavaMail + Postfix的AuthenticationFailedException问题

你遇到的javax.mail.AuthenticationFailedException核心原因很明确:你的JavaMail客户端尝试连接Postfix SMTP服务器时,要么没提供必要的认证信息,要么Postfix的配置要求认证但你没做对应设置。

关于你问的「凭证对应的具体是什么」

这得看你的Postfix配置情况:

  • 如果Postfix是本地运行在你的VPS上,且允许来自127.0.0.1的本地连接无需认证,那你可能根本不需要用户名密码——但前提是Postfix的配置信任本地请求。
  • 如果Postfix开启了smtpd_sasl_auth_enable = yes(即强制SMTP认证),那么这里的凭证就是你VPS上的系统用户信息:比如你代码里发件人用的root@mydomain,对应的就是VPS上root用户的密码;或者你专门为邮件服务创建的认证账号。

代码修改方案

方案1:尝试本地无认证发送(适合Postfix信任本地连接的场景)

把SMTP主机改成localhost,因为Postfix默认通常会信任来自本地的请求,无需认证:

public class SendMail {
    public void send(String user){
        final Logger logger = Logger.getLogger(SendMail.class);
        String to = "myemail@gmail.com";
        String from = "root@mydomain";
        // 替换为localhost,走本地连接
        String host = "localhost";
        
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host);
        // 如果测试时遇到TLS问题,可以暂时注释这两行先验证基础功能
        properties.setProperty("mail.smtp.starttls.enable", "true");
        properties.setProperty("mail.smtp.port", "587");
        
        Session session = Session.getInstance(properties);
        
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("This is the Subject Line!");
            // 注意你代码里的text变量未定义,这里补上示例内容
            message.setText("Test email content from local Postfix");
            
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            logger.error("This is error : "+mex);
            mex.printStackTrace();
        }
    }
}

方案2:添加SMTP认证信息(适合Postfix强制要求认证的场景)

如果Postfix必须要认证,你需要开启JavaMail的认证功能,并传入对应的账号密码:

public class SendMail {
    public void send(String user){
        final Logger logger = Logger.getLogger(SendMail.class);
        String to = "myemail@gmail.com";
        String from = "root@mydomain";
        String host = "mydomain";
        // 填入你的SMTP账号(可以是root或专门的邮件用户)和对应密码
        String username = "root";
        String password = "your-vps-root-password";
        
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host);
        properties.setProperty("mail.smtp.starttls.enable", "true");
        properties.setProperty("mail.smtp.port", "587");
        // 开启SMTP认证
        properties.setProperty("mail.smtp.auth", "true");
        
        // 创建带认证逻辑的Session
        Session session = Session.getInstance(properties, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("This is the Subject Line!");
            message.setText("Test email content with SMTP auth");
            
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            logger.error("This is error : "+mex);
            mex.printStackTrace();
        }
    }
}

额外的Postfix配置检查

登录你的VPS,检查/etc/postfix/main.cf文件,确保配置符合预期:

  • 确认mynetworks包含127.0.0.1,保证本地连接被信任:mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
  • 如果不需要认证,确保smtpd_sasl_auth_enable = no;如果需要认证,设置为yes并确认cyrus-sasl等SASL服务已安装配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:59:49