当前位置: 首页 > 知识库问答 >
问题:

尝试在 Java 中通过 SMTP 发送电子邮件,但收到错误“身份验证登录失败”

姚海
2023-03-14

在过去的几天里,我一直在用我的头撞我的桌子,试图弄清楚为什么我在尝试实现通过公司的Outlook服务器发送电子邮件的Java代码时不断收到以下错误:

DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
Exception in thread "main" javax.mail.AuthenticationFailedException: 535 5.7.0 authentication failed
    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:932)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:843)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:748)
    at javax.mail.Service.connect(Service.java:388)
    at javax.mail.Service.connect(Service.java:246)
    at javax.mail.Service.connect(Service.java:195)
    at javax.mail.Transport.send0(Transport.java:254)
    at javax.mail.Transport.send(Transport.java:124)

下面是有问题的代码:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail3 {

    public static void main(String[] args) throws Exception {

        Login e = new Login();

        String host = "mail.authsmtp.com";
        String port = "465";
        final String userName = e.getEmailUser();
        final String password = e.getEmailPassword();
        String toAddress = "first.last@email";
        String subject = "TEST";
        String message = "This is a test email";

        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.debug", "true");
        // properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.user", userName);
        properties.put("mail.password", password);

        // creates a new session with an authenticator
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        };
        Session session = Session.getInstance(properties, auth);

        // creates a new e-mail message
        Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(userName));
        InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
        msg.setRecipients(Message.RecipientType.TO, toAddresses);
        msg.setSubject(subject);
        msg.setSentDate(new Date());

        // creates message part
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(message, "text/html");

        // creates multi-part
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        // sets the multi-part as e-mail's content
        msg.setContent(multipart);

        // sends the e-mail
        Transport.send(msg);

    }

}

该错误在 Transport.send(msg) 触发。任何指导将不胜感激。

共有2个答案

闾丘坚诚
2023-03-14

535代码代表错误的用户名或密码。

通过调试此代码,确保您的电子邮件用户和密码正确。

另一个提示是通过网络登录邮件,或者尝试更改邮件密码。

慕容光启
2023-03-14

您需要访问https://myaccount.google.com,请确保安全,并将访问权限更改为较不安全的app.gmail。smtp需要最新的版本认证,这就是它无法工作的原因。

 类似资料:
  • 我正在尝试使用office 365帐户设置我们的无回复电子邮件通知。我已向管理员请求取消选中SMTP验证并创建了应用程序密码,但仍然在发送电子邮件时出错。电子邮件有MFA SMTP配置 用户名-无回复电子邮件帐户 仍然在下面抛出异常 SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.7.57客户端未通过发送邮件的身份验证。[HK2PR02CA0208.apcprd02.pro

  • 我正在尝试设置一些代码以通过Office 365经过身份验证的SMTP服务发送电子邮件: 这似乎不起作用,我看到了一个例外: SMTP服务器需要安全连接,或者客户端没有经过身份验证。服务器响应是:5 . 7 . 57 SMTP;客户端未通过身份验证,无法在系统中从< br >发送匿名邮件。net . mail . mail command . send(SmtpConnection conn,By

  • 我正在尝试通过smtp发送邮件,但它显示错误消息。我正在使用主机号578.after执行程序,它显示无法将套接字转换为TLS;嵌套异常是:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPath

  • 我通过SMTP服务发送电子邮件时收到以下错误消息: 我的系统上有以下SMTP设置: 操作系统:Windows 7家庭高级版 IIS:IIS 7 如何解决这个问题? 谢谢。

  • 我使用的PHPMailer表格在这里找到。 从网站下载的示例是“接触-3”,在引导主题中使用PHPMailer通过gmail发送SMTP电子邮件。当我使用“接触-1”时,它完全适用于我的托管域电子邮件地址,但SMTP版本适用于gmail地址,联系人表单不会提交。 在下面的代码中,我更改了以下行以添加我的gmail地址和gmail密码: 任何关于使用给定信息进行此工作的帮助都将不胜感激-提前感谢!

  • 我目前拥有的代码是: 我试图发送到的电子邮件地址托管在Office 365的Outlook上。我们可能需要稍后更改特定地址,但它们可能配置相同。 但是,每当我尝试运行命令时,都会收到同样的错误,错误的全文是: 我已经尝试了一些不同的方法,比如在25到587之间切换端口,将主机更改为office365的,或者将和切换为true或false。但是我总是看到同样的错误。我还遗漏了什么吗?