在 java 中发送电子邮件时抛出 javax.mail.AuthenticationFailedException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25341198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:09:37  来源:igfitidea点击:

javax.mail.AuthenticationFailedException is thrown while sending email in java

java

提问by Mahira Khan

I am beginner in java and I want to send an email in java, for that I am using this code in Java. But my code is throwing an exception, and I need a heads-up why…

我是 Java 初学者,我想用 Java 发送一封电子邮件,因为我在 Java 中使用此代码。但是我的代码抛出了一个异常,我需要知道为什么......

This is stack trace of exception:

这是异常的堆栈跟踪:

javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbsNX
534-5.7.14 No6jJbDc4l7fZ_WLdBD0sNHIIp_nLvplRMm0bYFBnZBF_XOyVvNSdd1FenDZJPwBTFQyRH
534-5.7.14 lriPK3myMm-dXkW3zK0-6XpO7BzI8hfRcByG1k7YiVzXlddTvs7QhjtgCWNcrzMBuPhoof
534-5.7.14 GjME2TgYzXJVHz5MV98nRnr_kq-kP7RmgOtX3IQHLwM5E8QGBC9-2THVQr_Ch_U0-1nZsc
534-5.7.14 yoPuNEw> Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 wr6sm26888533wjc.24 - gsmtp

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:892)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:814)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:728)
    at javax.mail.Service.connect(Service.java:364)
    at javax.mail.Service.connect(Service.java:245)
    at SendEmail.sendFromGMail(SendEmail.java:50)
    at SendEmail.main(SendEmail.java:18)
sent

This is my code

这是我的代码

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

public class SendEmail {

    private static String USER_NAME = "me";
    private static String PASSWORD = "xyz";
    private static String RECIPIENT = "[email protected]";

    public static void main(String[] args) {
        String from = USER_NAME;
        String pass = PASSWORD;
        String[] to = { RECIPIENT };
        String subject = "Java send mail example";
        String body = "Welcome to JavaMail!";

        sendFromGMail(from, pass, to, subject, body);
        System.out.println("sent");
    }

    private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
        Properties props = System.getProperties();
        String host = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);

        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            for( int i = 0; i < to.length; i++ ) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for( int i = 0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch (AddressException ae) {
            ae.printStackTrace();
        }
        catch (MessagingException me) {
            me.printStackTrace();
        }
    }
}

回答by Sazib

I had the same problem! Google prevents access for less secure apps.

我有同样的问题!Google 会阻止访问安全性较低的应用程序。

This is how I solved it:

我是这样解决的:

回答by biniam

  1. Make sure Less secure apps is TURNED ONhttps://www.google.com/settings/security/lesssecureappsenter image description here
  2. Allow each app to send email Go to https://accounts.google.com/b/0/DisplayUnlockCaptchaand click on Continue. enter image description here
    enter image description here
  3. This time: you can use your app to send email and all operations are allowed.
  1. 确保安全性较低的应用程序已打开https://www.google.com/settings/security/lesssecureapps在此处输入图片说明
  2. 允许每个应用程序发送电子邮件转到https://accounts.google.com/b/0/DisplayUnlockCaptcha并单击继续。 在此处输入图片说明
    在此处输入图片说明
  3. 这次:您可以使用您的应用发送电子邮件,并且允许所有操作。

More links:
https://support.google.com/accounts/answer/6010255https://productforums.google.com/forum/#!topic/gmail/9KCgzXY4G_c

更多链接:
https: //support.google.com/accounts/answer/6010255 https://productforums.google.com/forum/#!topic/gmail/9KCgzXY4G_c

回答by Sarfraz Khan

I am also facing same problem.

我也面临同样的问题。

Google does't give you direct open port access. If you are using your your google account for mail sending please Turn on the setting by clicking on this link. google go here : https://www.google.com/settings/security/lesssecureappsBest of luck hope it will work for you as well.

Google 不会为您提供直接的开放端口访问权限。如果您使用您的 google 帐户发送邮件,请单击此链接打开设置。谷歌去这里:https: //www.google.com/settings/security/lesssecureapps祝你好运,希望它也适用于你。

回答by Srini Kancharla

Its not just about enabling the "less secure apps" from google admin page.

它不仅仅是从谷歌管理页面启用“不太安全的应用程序”。

The above fix did not solve the issue for me. I contacted google and they asked me to add couple of TXT records to my DNS list. You will need a DKIM key that you will need to generate from the G Suite admin settings for gmail.

上述修复并没有为我解决问题。我联系了谷歌,他们要求我将几个 TXT 记录添加到我的 DNS 列表中。您将需要一个 DKIM 密钥,您需要通过 G Suite 管理设置为 Gmail 生成该密钥。

回答by SuperUser Sudo

  1. allow less secure apps from your google account settings https://www.google.com/settings/security/lesssecureapps
  2. open your gmail> goto settings > IMAP/POP settings > enable IMAP
  3. goto this url and turn this on (completing all these steps will surely work) https://www.google.com/accounts/DisplayUnlockCaptcha
  1. 允许从您的 Google 帐户设置中使用安全性较低的应用 https://www.google.com/settings/security/lesssecureapps
  2. 打开您的 Gmail > 转到设置 > IMAP/POP 设置 > 启用 IMAP
  3. 转到此网址并打开它(完成所有这些步骤肯定会起作用) https://www.google.com/accounts/DisplayUnlockCaptcha

official guide for help

官方帮助指南

https://support.google.com/mail/answer/7126229?hl=en-GB&visit_id=637246238859828954-474780158&rd=2

https://support.google.com/mail/answer/7126229?hl=zh-CN&visit_id=637246238859828954-474780158&rd=2