Google禁用“较不安全的应用程序”是否有工作?

发布于 2025-02-03 20:27:07 字数 560 浏览 2 评论 0 原文

因此,自从5月31日Google录制了“不太安全应用程序”的选项以来,我一直在使用Java Mail API,并且由于更新,因此我无法再使用Gmail SMTP发送电子邮件。

这是我遇到的错误:

javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials n13-20020a5d400d000000b0020ff7246934sm4970874wrp.95 - gsmtp

我切换到Outlook Mail,它似乎可以正常工作,但是我想知道是否有一种方法可以使用Gmail帐户

较少安全的应用程序&您的Google帐户

So since the 31 of may google has disabled the option for "Less secure apps", so I have been using the Java mail API, and since the update i can no longer send emails using the Gmail smtp.

This is the error I'm getting:

javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials n13-20020a5d400d000000b0020ff7246934sm4970874wrp.95 - gsmtp

I switched to an outlook mail and it seems to work fine, but i was wondering if there is a way to use a Gmail account

Less secure apps & your Google Account

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浪漫人生路 2025-02-10 20:27:07

您可以通过“应用程序密码”尝试身份验证。

在您的Google帐户上:

  1. set 2-Step验证 on

  2. 创建16-Character“ App” App password(
    如何创建应用程序密码) - >结果应类似于:
    16-character“ app password”

  3. 而不是Google帐户密码使用16-Character密码< /p>

      mailMessage mail = new MailMessage();
    foreach(dolociprejemnike()中的字符串接收器)
        mail.to.add(接收者);
    mail.from = new MailAddress(“ 重播”); //pošiljatelj(vedno enak)
    mail.subject = setSubject();
    mail.body = setBody();
    mail.isbodyhtml = true;
    
    smtpclient smtp = new smtpclient();
    smtp.host =“ smtp.gmail.com”;
    smtp.port = 587;
    smtp.usedefaultcredentials = true;
    smtp.credentials = new System.net.networkcredential(“ <​​a href =”/cdn-cgi/l/电子邮件保护“ class” class =“ __ cf_email__” data-cfemail =“ 09687979566e6464686065496065496e65496e64686860652222266666644.644.644  


You could try authentification via "App password".

On your Google account:

  1. set 2-Step Verification ON

  2. create 16-character "App password"(
    How to create app password) -> result should be similar to:
    16-character "App password"

  3. Instead of Google account password use 16-character password

    MailMessage mail = new MailMessage();
    foreach (string receiver in DolociPrejemnike())
        mail.To.Add(receiver);
    mail.From = new MailAddress("[email protected]", "No replay"); //pošiljatelj (vedno enak)
    mail.Subject = SetSubject();
    mail.Body = SetBody();
    mail.IsBodyHtml = true;
    
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "xtqapucsmyvqmvxp"); // Enter seders User name and password  
    smtp.EnableSsl = true;
    smtp.Send(mail);
    
幽蝶幻影 2025-02-10 20:27:07

因此,感谢您的所有重播!我通过这样做解决了这个问题:

我启用了“ Windows机器的应用程序密码”
然后,我只是将密码从电子邮件密码更改为Google生成的密码

,然后将代码更改为以下内容:

public class JavaMailUtil {
public static void sendMail(String recepient,String order) throws Exception {

    Properties properties=new Properties();
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");
    String myAccountEmail="[email protected]";
    String password="Generated Windows machine password from google";
    Session session=Session.getInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(myAccountEmail, password);
        }
    });
    
    Message message=prepareMessage(session,myAccountEmail,recepient,order);
    Transport.send(message);
    System.out.println("Message Sent successfully");
}

private static Message prepareMessage(Session session,String from,String to,String orderInfo) {
    Message message = new MimeMessage(session);
    try {
        
        message.setFrom(new InternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));a
        message.setSubject("Your subject here");
        message.setText(");
        return message;
    } catch (AddressException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

}

So thanks for all the replays! i have fixed this issue by doing this:

I have enabled the "App password for windows machines"
Then i simply changed the password from the email password to the google generated one

and changed the code to the following:

public class JavaMailUtil {
public static void sendMail(String recepient,String order) throws Exception {

    Properties properties=new Properties();
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");
    String myAccountEmail="[email protected]";
    String password="Generated Windows machine password from google";
    Session session=Session.getInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(myAccountEmail, password);
        }
    });
    
    Message message=prepareMessage(session,myAccountEmail,recepient,order);
    Transport.send(message);
    System.out.println("Message Sent successfully");
}

private static Message prepareMessage(Session session,String from,String to,String orderInfo) {
    Message message = new MimeMessage(session);
    try {
        
        message.setFrom(new InternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));a
        message.setSubject("Your subject here");
        message.setText(");
        return message;
    } catch (AddressException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

}

怎会甘心 2025-02-10 20:27:07

现在,您无法再使用Googles SMTP服务器使用登录和密码。唯一的选择是使用“ noreferrer”> xoauth2

我以前没有使用过雅加达,但它似乎支持它。您应该查看 oauth2支持

Properties props = new Properties();
props.put("mail.imap.ssl.enable", "true"); // required for Gmail
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", username, oauth2_access_token);

应用程序密码password

选项两个要转到您的Google帐户并生成一个 apps password

运行代码时,请使用生成的密码而不是实际用户密码。这是其中的主要问题,没有告诉Google将继续支持应用程序密码多长时间。

Now that you can no longer use login and password with Googles smtp server the only option really is to use XOauth2

I havent used Jakarta before but it appears to support it. You should look into OAuth2 Support

Properties props = new Properties();
props.put("mail.imap.ssl.enable", "true"); // required for Gmail
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", username, oauth2_access_token);

Apps password

option two is to go to your google account and generate an apps password

When running your code use the password generated instead of the actual users password. The main issue with this being there is no telling how long google will continue to support apps password.

吻安 2025-02-10 20:27:07

注意:在继续之前,请在Google帐户中启用2因子身份验证。

较不安全的应用程序( https://myaccount.google.com/u/0/u/0/secureapps )选项不再可用。

而是使用另一种使用Google提供的AppPassword的方式。
https://myaccount.google.com/u/0/apppasswords

由Google提供的代码而不是密码,这将用作身份验证令牌。

Note: please enable 2-factor authentication in google account before proceeding.

Less secure apps (https://myaccount.google.com/u/0/lesssecureapps) options is no longer available.

Instead use another way of using apppasswords provided by google.
https://myaccount.google.com/u/0/apppasswords

Use 16 digit code provided by google instead of password and that will serve as authentication token.

enter image description here

水中月 2025-02-10 20:27:07

对于那些遵循其他答案但仍会在使用应用程序密码时仍会遇到“身份验证失败”错误的人,一个关键是,如果您使用该解决方案,则该解决方案不适用于XOAUTH2,或者遵循指南的指南,说使用OAuth2。

因此,在以下代码中:

props.put("mail.imap.auth.mechanisms", "XOAUTH2");

只需将其更改为以下内容:

props.put("mail.imap.auth.mechanisms", "XOAUTH");

它应该起作用,使所有其他方面都保持不变。

To those who followed the other answers but are still getting the "Authentication Failed" error when using an app password, a key point is that this solution is NOT working for XOAUTH2 if you are using that or are following a guide saying to use oauth2.

So in the following code:

props.put("mail.imap.auth.mechanisms", "XOAUTH2");

Simply change it to the following:

props.put("mail.imap.auth.mechanisms", "XOAUTH");

and it should work, keeping all else the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文