【问题标题】:javaMail not working with OAuth gmail send emailjavaMail 不使用 OAuth gmail 发送电子邮件
【发布时间】:2021-02-17 00:16:47
【问题描述】:

javaMail:使用 google oauth 发送邮件导致 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);

oauth 访问令牌有效,刚刚刷新,大约在运行代码前 20 秒。

错误:

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

日志记录:

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

【问题讨论】:

    标签: oauth-2.0 oauth gmail google-oauth jakarta-mail


    【解决方案1】:

    我认为您需要禁用普通身份验证才能强制 Jakarta Mail 使用 XOAUTH2。

    props.put("mail.smtp.auth.plain.disable", "true");

    【讨论】:

    • 已经指定的机制:props.put("mail.smtp.auth.mechanisms", "XOAUTH2");我会试一试的。
    【解决方案2】:

    尝试以下代码:

            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:使用机制 XOAUTH2 授权 XOAUTH2 235 2.7.0 接受 调试:getProvider() 返回 javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP:需要用户名和密码进行身份验证 调试 SMTP:protocolConnect 返回 false,host=smtp.gmail.com,user=username,password=

    【讨论】:

    • 是的。代码正在运行,但我收到了我描述的错误。
    • 它仍然要求输入用户名和密码,OAUTH 被忽略。无法在线找到工作示例。
    • 同样的问题,gmail oauth2 没有任何作用。
    • Java 邮件在发布之前应该有一个 oauth 的工作示例。
    猜你喜欢
    • 2014-07-26
    • 1970-01-01
    • 2012-04-27
    • 2018-12-18
    • 2015-05-21
    • 2022-08-14
    • 2013-01-08
    • 1970-01-01
    相关资源
    最近更新 更多