gpt4 book ai didi

java - 无法使用 xoauth 在 javamail 上向 gmail 发送电子邮件

转载 作者:行者123 更新时间:2023-12-01 04:53:51 26 4
gpt4 key购买 nike

我一直在从事一个项目,该项目涉及连接 gmail smtp 并使用基于 oauth2 的连接发送电子邮件。

我正在使用提供的示例here连接到 gmail smtp 并连接到 smtp 工作正常。但是问题出在发送消息时。我遇到以下错误

com.google.code.com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 o5sm11801729pay.5

我不太清楚为什么在使用 gmail smtp 成功进行身份验证后会弹出错误。下面是完整的调试日志

DEBUG: setDebug: JavaMail version 1.4.4
DEBUG SMTP: enable SASL
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 mx.google.com ESMTP th10sm14683815pbc.76
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO bala-Inspiron-N4010
250-mx.google.com at your service, [27.97.28.232]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO bala-Inspiron-N4010
250-mx.google.com at your service, [27.97.28.232]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
AUTH XOAUTH2 dXNlcj1jaGFuZHUyNzA4QGdtYWlsLmNvbQFhdXRoPUJlYXJlciB5YTI5LkFIRVM2WlFqUWJqZ3RKRmpSSlFxM2lrMnd1QXMySEQ1UXVjWm8ySFN1bnMwWlk2VAEB
235 2.7.0 Accepted
HELO
250 mx.google.com at your service
NOOP
250 2.0.0 OK th10sm14683815pbc.76
DEBUG: getProvider() returning com.google.code.javax.mail.Provider[TRANSPORT,smtp,com.google.code.com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: enable SASL
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 mx.google.com ESMTP qf7sm14690532pbb.49
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO bala-Inspiron-N4010
250-mx.google.com at your service, [27.97.28.232]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO bala-Inspiron-N4010
250-mx.google.com at your service, [27.97.28.232]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<chandu2708@gmail.com>
530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qf7sm14690532pbb.49
DEBUG SMTP: got response code 530, with response: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qf7sm14690532pbb.49

RSET
250 2.1.5 Flushed qf7sm14690532pbb.49
com.google.code.com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qf7sm14690532pbb.49

下面是我用来发送电子邮件的代码

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.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");

props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
//props.put("mail.smtp.password", "");

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

Session session = Session.getInstance(props);
session.setDebug(debug);

final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
final String emptyPassword = null;
transport.connect(host, port, userEmail, emptyPassword);

byte[] response = String.format("user=%s\1auth=Bearer %s\1\1",
userEmail, oauthToken).getBytes();
response = BASE64EncoderStream.encode(response);

transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235);

return new SMTPConnection(transport, session, new String(response));

--------------XXX end of method XXX-----------------


SMTPConnection smtpConnection = XoauthAuthenticator.connectToSmtp("smtp.gmail.com", 587,
user.getEmail(), user.getAccessToken(), null, XoauthAuthenticator.getAnonymousConsumer(),
true);

SMTPMessage smtpMessage = new SMTPMessage(smtpConnection.getSession());
smtpMessage.setFrom(new InternetAddress(user.getEmail()));
smtpMessage.setRecipients(RecipientType.TO, InternetAddress.parse(email));

String subject = message;

smtpMessage.setSubject(subject);

smtpMessage.setText(message);

smtpMessage.setSentDate(new Date());


smtpConnection.getSMTPTransport().simpleCommand("HELO");

boolean isConnected = smtpConnection.getSMTPTransport().isConnected();

smtpConnection.getSMTPTransport().send(smtpMessage, smtpMessage.getAllRecipients());

非常感谢您的帮助。

谢谢你,-巴拉。

最佳答案

关于java - 无法使用 xoauth 在 javamail 上向 gmail 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14499968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com