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

标签 java email jakarta-mail

我想使用 JavaMailAPI 发送邮件

我已经完成了一些编码,但它无法抛出异常:-

消息发送失败javax.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();
    } 

}

服务器控制台出现异常

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

在 534 5.7.9 http://support.google.com/accounts/bin/answer.py?answer=185833 了解更多信息o5sm11464195pdr.50-gsmtp

谁能帮我解决这个问题。

最佳答案

您已启用 Two phase authentication用于您的 Google 帐户,因此应用程序将无法使用实际密码登录到您的 Google 帐户。 Google 希望您为您使用的每个应用程序生成一个应用程序专用密码(并为其命名),然后使用该密码从您的应用程序登录到您的 Google 帐户。这允许您在启用两步身份验证时不向第三方应用程序提供密码。

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

link清楚地说明要做什么。

关于JavaMail 异常 javax.mail.AuthenticationFailedException 534-5.7.9 需要特定于应用程序的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26594097/

相关文章:

java - Intellij IDEA Maven : can't resolve symbol "backtype"

php - 通过电子邮件链接预先填充 Paypal 金额

PHP 邮件脚本不发送文本区域并复制正文中的所有其他内容

java - 为 HTML 电子邮件正文错误编码希伯来语文本

java - 通过 JavaMail 发送到 gmail 时的 TLS 问题

java - 如何处理关系数据库中的继承

java - 将 JSON 数据映射到 Java 对象时遇到问题

java - 如何更改tomcat或jetty下的Java webapp 'WEB-INF path'约定

spring - Grails邮件插件:附加org.springframework.web.multipart.commons.CommonsMultipartFile

java - 如何正确设置 JavaMail 超时设置