java - 无法使用java代码从gmail发送邮件

标签 java email smtp gmail jakarta-mail

package abc;

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

public class SendMail {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String to="to@gmail.com";
        String from="from@gmail.com";
        final String username="from";
        final String password="password";
        Properties properties=new Properties();
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.port", "587");

        Session session=Session.getInstance(properties,
                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("Hey I wrote a java code to send mail. Thanks ");
            Transport.send(message);
            System.out.println("Sent Mail :)");
        }       
        catch(MessagingException e){
            e.printStackTrace();
        }

    }

}

当我尝试运行上面的代码时出现以下错误::

javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvHc
534-5.7.14 gCjfbim5WYCDTQee6sZlZ2d31nncueOizOcz9NieexN5nSlGr0c49lSZ43qc4RQPQvpWLH
534-5.7.14 qCUQUecjIR7qxdYJ5R_WgLxkzD9u4Ds3EEG7ceSMyTZg0dpSGJb-zl5C82YDTdLOYTX5Pl
534-5.7.14 tmEfWrmktFCdAxUjtDiPNruDLqhPSIZ9dd187tQjBtOw2X8zx7MUcysN9BRawwDmbXT6mJ
534-5.7.14 cLn_sJS5UBuqommn0uJK7W1tPZzU> 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/answer/78754 qy7sm48619995pab.34 - gsmtp

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:648)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
    at javax.mail.Service.connect(Service.java:313)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at abc.SendMail.main(SendMail.java:36)

Gmail 不允许使用此代码。我什至收到了一封来自 gmail 的关于可疑登录的邮件,并检查了我运行此代码的位置。解决办法是什么?

最佳答案

这是AuthenticationFailedException,您应该使用真实的用户名和密码组合才能使程序正常运行,并且应该授予程序访问权限以登录gmail,具体说明如下.

而且您还应该检查一下以允许访问您的程序来发送邮件,堆栈跟踪中给出的链接中提供了完整的答案;

https://support.google.com/mail/answer/78754

您需要做的是(如上面的链接中所述);

  1. 授予您的用户访问权限;

https://accounts.google.com/DisplayUnlockCaptcha

enter image description here

  • 允许访问安全性较低的登录;
  • https://www.google.com/settings/security/lesssecureapps

    enter image description here

    此后,如果您再次尝试使用有效参数运行代码,则会发送测试邮件。

    关于java - 无法使用java代码从gmail发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35737840/

    相关文章:

    java - 通过 Java 和 Postfix 服务器发送 SMTP 邮件

    java - 我的邮件 DKIM-Signature 凭空创建一个电子邮件地址

    java - 如果将 SharedPreferences 放在那里,代码将不起作用

    java - 日期时间在应用程序中正确但在 mysql [时区] 中错误

    java - 从java中的arrayList中删除一个项目

    php - Woot.com 的电子邮件分享按钮如何递增?

    c# - 将用户名/密码传递给 SmtpClient (.NET) 的正确方法

    java - 区分水陆的算法

    php - 在 one.com 上使用 smtp 和 php

    php - 我怎样才能自动为我的网站成员(member)创建一个电子邮件地址?