1

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

我使用此处提供的示例连接到 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());

非常感谢您的帮助。

谢谢你,-巴拉。

4

1 回答 1

0

您使用了 Transport.send 而不是 Transport.sendMessage。

于 2013-01-24T17:47:27.503 回答