javaMail不适用于OAuth gmail发送电子邮件 [英] javaMail not working with OAuth gmail send email

查看:124
本文介绍了javaMail不适用于OAuth gmail发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javaMail:使用Google oauth发送邮件导致javax.mail.AuthenticationFailedException.

javaMail: send mail using google oauth caused javax.mail.AuthenticationFailedException.

Properties props = new Properties();
props.put("mail.smtp.ssl.enable", "true"); // required for Gmail
props.put("mail.smtp.auth.mechanisms", "XOAUTH2");

Session session = Session.getInstance(props);
Transport transport = session.getTransport("smtps");
transport.connect("smtp.gmail.com", 465, xyz@gmail.com, oauth_access_token);

在运行代码之前大约20秒,oauth访问令牌是有效的,只是刷新了.

The oauth access token is valid, just refreshed, about 20 seconds before running the code.

错误:

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 s136sm158338qka.106 - gsmtp

        at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:965)
        at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:876)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:780)
        at javax.mail.Service.connect(Service.java:366)

从日志记录来看,似乎auth使用的是用户名/密码,而不是OAUTH.指南: https://javaee.github.io/javamail/OAuth2

From the logging, it seems that auth is using username/password, not OAUTH. guide: https://javaee.github.io/javamail/OAuth2

记录:

DEBUG: JavaMail version 1.6.2
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
220 smtp.gmail.com ESMTP s136sm158338qka.106 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: protocolConnect login, host=smtp.gmail.com, user=xyz@gmail.com, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed

推荐答案

尝试以下代码:

        Properties props = new Properties()
            props.setProperty("mail.smtp.host", "smtp.gmail.com")
            props.setProperty("mail.smtp.port", "587")
            props.put("mail.smtp.auth","true")
            props.put("mail.smtp.starttls.enable", "true")
            props.put("mail.smtp.starttls.required", "true")
            props.put("mail.smtp.auth.mechanisms", "XOAUTH2")
            props.put("mail.smtp.auth.login.disable", "true")
            props.put("mail.smtp.auth.plain.disable", "true")
            props.put("mail.smtp.auth.ntlm.disable", "true")
            props.put("mail.debug", "true")
            props.put("mail.debug.auth", "true")
            Session session = Session.getInstance(props)
            String emptyPassword = ""
            final URLName unusedUrlName = null
            SMTPTransport transport = new SMTPTransport(session, unusedUrlName)
            transport.connect("smtp.gmail.com", googleEmail, googleAccessToken)\
            Multipart multipart = new MimeMultipart()
            BodyPart messageBodyPart = new MimeBodyPart()
            def from = googleEmail
            def to = "test@test.com"
            def subj = "TestOath2Gmail"
            def body = "TTT"
            message.setFrom(new InternetAddress(googleEmail))
            message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to))
            Date sentDate = new Date()
            message.setSentDate(sentDate)
            message.setSubject(subj)
            messageBodyPart.setContent(body, "text/html")
            multipart.addBodyPart(messageBodyPart)
            message.setContent(multipart)
            transport.send(message)

获取错误:调试SMTP:使用机制XOAUTH2AUTH XOAUTH2235 2.7.0接受调试:getProvider()返回javax.mail.Provider [TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]调试SMTP:需要用户名和密码进行身份验证调试SMTP:协议连接返回false,host = smtp.gmail.com,用户=用户名,密码=

Getting error: DEBUG SMTP: Using mechanism XOAUTH2 AUTH XOAUTH2 235 2.7.0 Accepted DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: need username and password for authentication DEBUG SMTP: protocolConnect returning false, host=smtp.gmail.com, user=username, password=

这篇关于javaMail不适用于OAuth gmail发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆