【问题标题】:AuthenticationFailedException error while sending SMTP email发送 SMTP 电子邮件时发生 AuthenticationFailedException 错误
【发布时间】:2015-04-16 17:12:32
【问题描述】:

我尝试在 java 中发送 SMTP 电子邮件,但出现这样的错误并且我没有收到邮件。 我关闭了所有防火墙和防病毒软件。

错误:

javax.mail.AuthenticationFailedException: 534-5.7.14<https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbuIW

534-5.7.14 tAxHxbq4WR-vUuV1uOFAvx8NhInfxYTyNHi_8sJ80lX5lBdBla2ROiSKoysMNcFoQ6sGe
534-5.7.14 DUh173tDMolJ64W-Rahx1fhVF_08AvWrphibgQXiyyz5U1FNMMb-eGGJlUIbjyvBgQuZY6

534-5.7.14 tnykIXdVn__mg87aOmtxoss-EiFYeKdvuiBbt5eb9t_NOc97h-PkXOco-9FcYW69Iz9CTu

534-5.7.14 rfyhlo24k9oqIiWtcJwv85oUCO2g> Please log in via your web browser and

534-5.7.14 then try again.

534-5.7.14 Learn more at

534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 pd8sm1306363pdb.93 - gsmtp   

这是我的代码:

private void btn_mailActionPerformed(java.awt.event.ActionEvent evt) {                                         

    String to = "receive.address@gmail.com";

    String from = "send.address@gmail.com";
    final String username = "send.address";
    final String password = "sendpassword";
    String host = "smtp.gmail.com";
    Properties pro = new Properties();
    pro.put("mail.smtp.host",host);
    pro.put("mail.smtp.socketFactory.port","465");
     pro.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    pro.put("mail.smtp.auth","true");
     pro.put("mail.smtp.port","465");
    Session session = Session.getInstance(pro,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication()
                {
                    return new PasswordAuthentication(username,password);
                }
            }

            );
    try 
    {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject("test mail");
        message.setText("Hello how are you?");
        Transport.send(message);
        JOptionPane.showMessageDialog(null,"Send");

    } 
    catch (Exception e) {
        JOptionPane.showMessageDialog(null,e.toString());
        System.out.println(e.toString());
    }

【问题讨论】:

标签: java smtp jakarta-mail


【解决方案1】:

从浏览器登录电子邮件并转到this page。你会看到这个;

确保点击“开启”并再次尝试您的代码。

【讨论】:

  • 谢谢。我不得不将其关闭,但它仍然注意到该错误。
  • 你想打开它,而不是关闭它。您的应用程序是否与您使用的浏览器在同一台机器上运行?
  • 我尝试打开和关闭。是的,我的应用程序与我正在使用的浏览器在同一台机器上运行。我认为代码是正确的,但 gmal 不允许这样发送电子邮件。
  • @nistelrooy41001662 我想这应该被接受为答案!
【解决方案2】:

我有同样的问题。在对 Gmail 进行大量测试后,我发现问题在于 Gmail 需要 OAuth 登录,而不仅仅是密码。解决方案是使用Gmail API。但是,这是一个非常复杂的解决方案,我不会详细介绍。如果您对此感兴趣,请阅读第一个答案here

但是,如果您想要一个简单的解决方案,我所做的只是切换到雅虎帐户。因为雅虎不使用相同的加密,所以它工作得很好。注意:不要忘记将 SMTP 服务器更改为“smtp.mail.yahoo.com”,并将端口更改为“25”。

如果您想从头开始设置,只需按照this 教程下载JavaMail APIJava Activation Framework

然后您可以复制并粘贴我的代码,更改顶部变量,一切都会正常运行!如果我错过了什么,请告诉我!谢谢!

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

public class Mailer {

    public static void main(String[] args) {
        final String username = "your-email@yahoo.com";
        final String password = "your-password";
        final String recipient = "email-recipient";
        final String subject = "message-subject";
        final String emailmessage = "message";

        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "smtp.mail.yahoo.com");
        props.put("mail.smtp.port", "25");

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO,    InternetAddress.parse(recipient));
            message.setSubject(subject);
            message.setText(emailmessage);

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

    }

}

【讨论】:

  • 您所说的对于 Java 来说可能是正确的,但我不确定,因为在其他平台上,Google 不需要 OAuth 来使用他们的 SMTP。
  • 好吧,如果你想使用谷歌,据说你可以turn on access for less secure apps,但我无法让它为我工作。这就是我改用雅虎的原因。无论如何,我还没有听说某些事情不需要 OAuth。也许它只是与Java
  • @user4775991 我怀疑上面的代码 sn-p 现在是否适用于雅虎的双重登录身份验证
猜你喜欢
  • 1970-01-01
  • 2011-07-30
  • 2016-08-24
  • 2015-02-21
  • 1970-01-01
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多