JavaMail异常javax.mail.AuthenticationFailedException 534-5.7.9需要特定于应用程序的密码

Sud*_*yak 6 java email jakarta-mail

我想使用JavaMailAPI发送邮件

我做了一些编码,但它没有工作抛出异常: -

消息发送Failedjavax.mail.AuthenticationFailedException:534-5.7.9需要特定于应用程序的密码.

package com.appreciationcard.service;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.appreciationcard.dao.DAOFactory;
import com.appreciationcard.forms.ComposeForm;

public class MailServiceImpl implements MailService {

public boolean mailsent(ComposeForm composeForm) {
    String to = composeForm.getTo();
    String from = composeForm.getFrom();
    String cc = composeForm.getCc();
    String bcc = composeForm.getBcc();
    String subject = composeForm.getSubject();
    String messages = composeForm.getMessage();
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.port", "587");
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.debug", "true");
    System.out.println("Properties" + props);
    Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(
                            "tosudhansusekhar@gmail.com", "xxxx");
                }
            });
    try {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("dynamicmihir@gmail.com"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                to));
        message.setSubject(subject);
        message.setText(messages);
        Transport.send(message);
    } catch (MessagingException mex) {
        System.out.println("Message Sending Failed" + mex);
        mex.printStackTrace();
    } 

}
Run Code Online (Sandbox Code Playgroud)

}

我在服务器控制台上得到了Exception

消息发送Failedjavax.mail.AuthenticationFailedException:534-5.7.9需要特定于应用程序的密码.

如需了解更多信息,请访问534 5.7.9 http://support.google.com/accounts/bin/answer.py?answer=185833 o5sm11464195pdr.50 - gsmtp

愿任何人帮助我解决这个问题.

Vla*_*adi 32

更新至 2023 年


  1. 两步验证 - 您必须执行此步骤才能继续

在此输入图像描述

2)登录并添加电话号码并通过短信验证

绿色“V”后将变成 在此输入图像描述

3)按“两步验证”,向下至列表末尾,然后单击“应用程序密码

在此输入图像描述

  1. 然后在“选择应用程序”中输入应用程序的名称,然后按“创建”

在此输入图像描述

在打开的页面中写下描述并按生成

  1. 在打开的“生成的应用程序密码”中复制创建的密码并将其用作您的 smpt 密码,就像平常的密码一样(检查您不要复制空格)
    在此输入图像描述

如何使用 在您尝试设置的应用程序或设备中转至您的 Google 帐户设置。将您的密码替换为上面显示的 16 个字符的密码。就像您的普通密码一样,此应用程序密码授予您对 Google 帐户的完全访问权限。您不需要记住它,所以不要写下来或与任何人分享。

  • 这种方法在 2022 年有效 (4认同)

Sak*_*ket 14

您已为自己的Google帐户启用了两阶段身份验证,因此应用程序无法使用实际密码登录您的Google帐户.Google希望您为您使用的每个应用程序生成应用程序专用密码(并为其命名),然后使用该密码从您的应用程序登录到您的Google帐户.这使您在启用两步验证时不会将密码提供给第三方应用程序.

另一种方法是让您的应用程序支持重定向到Google页面,以使用Google身份验证器应用生成的用户名和密码以及代码进行身份验证.

链接清楚地解释了该做什么.


小智 10

只需为您的帐户创建一个应用程序密码并使用该密码即可。

创建密码的步骤:

转到您的帐户设置 ( https://myaccount.google.com/ ) -->> 安全 -->> 在登录 Google 下 -->> 应用程序密码 -->> 输入您的凭据以登录您的帐户 - ->> 选择“应用程序”和“设备”-->> 生成。

将密码复制并粘贴到某处。

您可以使用此密码代替您的帐户密码。