问题描述
我使用安全性较低的应用程序连接 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 (将#修改为@)