Java Mail - 电子邮件不通过 Gmail 发送

标签 java jsp jakarta-mail

我正在尝试执行以下代码集,但没有成功。我正在使用我的 Gmail 帐户。我禁用了两步验证,并且允许打开不太安全的应用程序,但我仍然收到错误:

javax.mail.AuthenticationFailedException: 534-5.7.14

public void sendEmail()
{
    try
    {
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props, new javax.mail.Authenticator()
        {
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="34514c5559445851745359555d581a575b59" rel="noreferrer noopener nofollow">[email protected]</a>", "password");
            }
        });

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1b4a9b0bca1bdb491b6bcb0b8bdffb2bebc" rel="noreferrer noopener nofollow">[email protected]</a>"));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e1b061f130e121b3e1d11130e1f1007501d1113" rel="noreferrer noopener nofollow">[email protected]</a>"));
        message.setSubject("Testing Subject");
        message.setText("Testing Text");

        Transport.send(message);

        System.out.println("Mail Sent!");

    } 

    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}

最佳答案

我所需要做的就是启用两步验证,然后为应用程序生成应用程序密码。感谢大家的回应。 :)

关于Java Mail - 电子邮件不通过 Gmail 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46006443/

相关文章:

java - Java中如何删除字符串中的重复空格并统计删除空格的数量?

java - Hibernate Spring - @OneToMany - 外键未存储在数据库中

java - Struts2 Web 应用程序中的 HTTP 状态 404

java - 使用 JSP 和表达式语言 (EL) 打印值

java - 如何在 JSP 中不使用 scriptlet 读取附件消息?

java - 电子邮件中的 itext PDF 附件

java - 为什么我的对象不需要调用 .toString 方法?

java - JavaFX WebEngine 中的 Google map 热图错误

java - 0 ,1 和空字符串的正则表达式模式

JavaMail Exchange 身份验证