Sos*_*ske 34 security email gmail jakarta-mail
因此,自 5 月 31 日起,谷歌禁用了“不太安全的应用程序”选项,因此我一直在使用 Java 邮件 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 邮件,似乎工作正常,但我想知道是否有办法使用 Gmail 帐户
Pet*_*arc 41
您可以尝试通过“应用密码”进行身份验证。
\n在您的 Google 帐户上:
\n将两步验证设置为开启
\n创建 16 个字符的“应用程序密码”(\n如何创建应用程序密码) -> 结果应类似于:\n 16 个字符的“应用程序密码”
\n使用 16 个字符的密码代替 Google 帐户密码
\nMailMessage mail = new MailMessage();\nforeach (string receiver in DolociPrejemnike())\n    mail.To.Add(receiver);\nmail.From = new MailAddress("app_gmail@gmail.com", "No replay"); //po\xc5\xa1iljatelj (vedno enak)\nmail.Subject = SetSubject();\nmail.Body = SetBody();\nmail.IsBodyHtml = true;\n\nSmtpClient smtp = new SmtpClient();\nsmtp.Host = "smtp.gmail.com";\nsmtp.Port = 587;\nsmtp.UseDefaultCredentials = true;\nsmtp.Credentials = new System.Net.NetworkCredential("app_gmail@gmail.com", "xtqapucsmyvqmvxp"); // Enter seders User name and password  \nsmtp.EnableSsl = true;\nsmtp.Send(mail);\n现在您不能再使用 Googles smtp 服务器的登录名和密码,唯一的选择就是使用XOauth2
我以前没有使用过 Jakarta,但它似乎支持它。您应该查看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);
选项二是转到您的谷歌帐户并生成应用程序密码
运行代码时,使用生成的密码而不是实际的用户密码。主要问题是,不知道谷歌将继续支持应用程序密码多久。
所以感谢所有的重播!我通过这样做解决了这个问题:
我启用了“Windows 机器的应用程序密码”然后我只需将密码从电子邮件密码更改为谷歌生成的密码
并将代码更改为以下内容:
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="yourEmailAddress@gmail.com";
    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;
}
}