首页 文章

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

提问于
浏览
1

我一直致力于一个项目,涉及连接gmail smtp和使用基于oauth2的连接发送电子邮件 .

我使用提供的样本here连接到gmail smtp并连接到smtp工作正常 . 但问题是在发送消息时 . 我低于错误

com.google.code.com.sun.mail.smtp.SMTPSendFailedException:530-5.5.1需要身份验证 . 如需了解更多信息,请访问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());

非常感谢您的帮助 .

谢谢你,-bala .

1 回答

相关问题