繁体   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