试图在Java中实现通过SMTP发送电子邮件,但出现错误";“身份验证登录失败”;

试图在Java中实现通过SMTP发送电子邮件,但出现错误";“身份验证登录失败”;,java,email,outlook,smtp,Java,Email,Outlook,Smtp,在过去的几天里,我一直在用我的头撞我的办公桌,试图弄明白为什么我在试图实现通过我公司的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

在过去的几天里,我一直在用我的头撞我的办公桌,试图弄明白为什么我在试图实现通过我公司的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);

    }

}

该错误在传输发送(msg)时触发。非常感谢您的指导。

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

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


另一个技巧是通过网络登录邮件,或者尝试将密码更改为邮件。

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

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


另一个技巧是通过web登录邮件,或者尝试将密码更改为邮件。

您需要访问、设置安全并更改不太安全的应用程序的访问权限。gmail.smtp需要最新版本的身份验证,这就是它无法工作的原因。

您需要访问,设置安全并更改不太安全的app.gmail.smtp的访问权限需要最新版本的身份验证,这就是它无法工作的原因。

您是否打印了e.getEmailUser()和e.getEmailPassword()并验证它们是否正确我知道e.getEmailPassword()是正确的(除非我的Outlook密码与我的Windows 7密码不同,这意味着我一直都错了)。e.getEmailUser()是“第一个”。last@company.com“,但我开始认为可能必须以不同的方式编写。您是否打印了e.getEmailUser()和e.getEmailPassword()并验证它们是否正确我知道e.getEmailPassword()是正确的(除非我的Outlook密码与我的Windows 7密码不同,这意味着我一直都错了)。e.getEmailUser()是“第一个”。last@company.com",但我开始认为可能必须用不同的方式来写。我的例子如何做我的例子如何做