簡體   English   中英

使用javax.mail通過Google oauth發送電子郵件

[英]Use javax.mail send email with google oauth

我正在嘗試使用javax.mail和Google OAuth發送電子郵件。 我的工作流程是:

使用Google OAuth登錄的用戶,獲取access_token,將令牌發送到Java后端服務器,然后使用javax.mail進行身份驗證,並通過電子郵件和令牌(而不是電子郵件和密碼)發送電子郵件。 但是,在道具中設置令牌后,仍然會收到無效的用戶名和密碼錯誤。 您有任何解決方法的想法嗎?

Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "true");
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.imaps.auth.login.disable", "true");
props.put("mail.imaps.auth.plain.disable", "true");
props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, accessToken);
Session session = Session.getInstance(props);
session.setDebug(true);
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
mimeMessage.setSubject("test");
mimeMessage.setContent("xxx", "text/html");


SMTPTransport transport = (SMTPTransport)session.getTransport("smtp");
transport.connect("smtp.gmail.com", myEmailAddress, "");
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM