【问题标题】:Javamail API exception - Authentication requiredJavamail API 异常 - 需要身份验证
【发布时间】:2014-09-11 20:40:35
【问题描述】:

我正在使用以下代码从

发送邮件
public class Test {

    private String SMTP_HOST_NAME = "smtp.bizmail.yahoo.com";
    private String SMTP_AUTH_USER = "mymail@domain.com";
    private String SMTP_AUTH_PWD = "mypassword";
    private String SMTP_MAIL_PORT = "587";  

    public void postMail(String recipient, String subject, String message)
    {
        Properties props = new Properties();

        SMTPAuthenticator authenticator = new SMTPAuthenticator();
        props.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.host", SMTP_HOST_NAME);
        props.setProperty("mail.smtp.port", SMTP_MAIL_PORT);
        props.setProperty("mail.smtp.starttls.enable","true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.debug", "true");

        Session session = Session.getInstance(props, authenticator);
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(SMTP_AUTH_USER));
            msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipient));
            msg.setSubject(subject);
            msg.setContent(message, "text/html");
            Transport.send(msg);
            System.out.println("Email send successfully Done");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }

    public static void main(String[] args) {
        Test s = new Test();
        s.postMail("sometestmail@test.com", "Test Subject", "Test Mail");
    }
}     

从上面的代码我可以成功发送电子邮件,但现在我收到异常

javax.mail.MessagingException: 530 5.7.1 Authentication required 

Yahoo smtp 服务器或其他方面是否有任何变化

这里是堆栈跟踪

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG: SMTPTransport trying to connect to host "smtp.bizmail.yahoo.com", port 587
DEBUG SMTP RCVD: 220 smtp.bizmail.yahoo.com ESMTP ready
DEBUG: SMTPTransport connected to host "smtp.bizmail.yahoo.com", port: 587
DEBUG SMTP SENT: EHLO ET-3-PC
DEBUG SMTP RCVD: 250-smtp.bizmail.yahoo.com
250-PIPELINING
250-SIZE 41697280
250-8 BITMIME
250 STARTTLS

DEBUG SMTP Found extension "PIPELINING", arg ""
DEBUG SMTP Found extension "SIZE", arg "41697280"
DEBUG SMTP Found extension "8", arg "BITMIME"
DEBUG SMTP Found extension "STARTTLS", arg ""
DEBUG SMTP: use8bit false
DEBUG SMTP SENT: MAIL FROM:<mailer@fundoospace.com>
DEBUG SMTP RCVD: 530 5.7.1 Authentication required

DEBUG SMTP SENT: QUIT
javax.mail.SendFailedException: Sending failed;
  nested exception is: 
    javax.mail.MessagingException: 530 5.7.1 Authentication required

    at javax.mail.Transport.send0(Transport.java:219)
    at javax.mail.Transport.send(Transport.java:81)
    at com.einsteiner.util.SendMailToUser.postMail(SendMailToUser.java:67)
    at com.einsteiner.util.SendMailToUser.main(SendMailToUser.java:197)

【问题讨论】:

  • 你从哪里得到异常?
  • 您的凭据可能无效。也可能是他们以这种方式拒绝您的联系。
  • @Makoto 早些时候我能够成功发送邮件而没有任何错误。但现在我得到了这个例外。在运行代码时,我将用户名和密码替换为实际用户名和密码。由于我在这里提到的原因 test.com 或 domain.com
  • 请尝试将身份验证凭据添加到用于电子邮件设置的属性中。例如props.setProperty("mail.smtp.password", SMTP_AUTH_PWD);和 props.setProperty("mail.smtp.username", SMTP_AUTH_USER);
  • @Utsav 我已经添加了这两个属性,但仍然得到相同的异常

标签: java email jakarta-mail


【解决方案1】:

根据调试输出,您使用的是 非常 旧版本的 JavaMail。这就是您设置的属性不起作用的原因。

您应该升级到更新的版本(至少 1.4.7,它支持这些属性)。您可以找到最新版本here

【讨论】:

    猜你喜欢
    • 2016-01-23
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多