微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Jakarta 邮件无法使用带有 XOAUTH2 令牌的 tls 或 ssl 连接到 gmail smtp

如何解决Jakarta 邮件无法使用带有 XOAUTH2 令牌的 tls 或 ssl 连接到 gmail smtp

我使用安全性较低的应用程序连接 gmail smtp 没有任何问题。 我对使用访问令牌的 IMAP 连接也没有任何问题。 但是 smtp 与 tocken 的连接对我来说是一个亮点。 我在 stackowerflow 上用谷歌搜索,但没有找到适合我的解决方案。

案例 1 尝试使用 tls

        Properties props = new Properties()
            props.setProperty("mail.smtp.host","smtp.gmail.com")
            props.setProperty("mail.smtp.port","587")
            props.put("mail.smtp.starttls.enable","true")
            props.setProperty("mail.smtp.ssl.protocols","TLSv1.1 TLSv1.2")
            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.login.disable","true")
            props.put("mail.smtp.auth.plain.disable","true")
            props.put("mail.debug","true");
            props.put("mail.debug.auth","true")
            Session session = Session.getInstance(props)
            Transport transport = session.getTransport("smtp")
            transport.connect("mail.smtp.host","smtp.gmail.com",googleEmail,googleAccesstoken)
            MimeMessage message = new MimeMessage(session)
            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)
            

在执行 transport.send 方法期间,我收到以下错误

调试 SMTP:得到响应代码 530,响应:530-5.7.0 需要身份验证。了解更多信息 530 5.7.0 https://support.google.com/mail/?p=WantAuthError b17sm826606lfi.57 - gsmtp

案例 2 尝试使用 ssl

        Properties props = new Properties()
            ...
            props.put("mail.smtp.ssl.enable","true")
            props.put("mail.smtp.auth","true")
            //props.put("mail.smtp.starttls.enable","true")
            //props.setProperty("mail.smtp.ssl.protocols","TLSv1.1 TLSv1.2")
            //props.put("mail.smtp.starttls.required","true")
            ...

出现以下错误: javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587; 嵌套异常是: javax.net.ssl.SSLException: 无法识别的 SSL 消息,纯文本连接?

我尝试使用不同的端口:587、465,但没有帮助。此外,使用参数但从未成功使用 SMTP 连接。 请指教。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。