因此,由于 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 帳戶
不太安全的應用和您的 Google 帳戶
uj5u.com熱心網友回復:
您可以嘗試通過“應用密碼”進行身份驗證。
在您的 Google 帳戶上:
將“兩步驗證”設定為開啟 兩步驗證
創建 16 字符的“應用程式密碼”( 如何創建應用程式密碼)-> 結果應類似于: 16 字符的“應用程式密碼”
使用 16 個字符的密碼代替 Google 帳戶密碼
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);
uj5u.com熱心網友回復:
現在您不能再在 Google 的 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);
應用密碼
選項二是轉到您的谷歌帳戶并生成應用程式密碼
運行代碼時,使用生成的密碼而不是實際的用戶密碼。主要問題是不知道谷歌將繼續支持應用程式密碼多久。
uj5u.com熱心網友回復:
所以感謝所有的重播!我通過這樣做解決了這個問題:
我已啟用“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="[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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/487990.html
