简体   繁体   English

使用OAuth在Android中发送电子邮件而无需用户交互

[英]Sending email without user interaction in android using OAuth

I am getting this error while trying to connect to smtp 尝试连接到SMTP时出现此错误

javax.mail.MessagingException: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==

Here is my code 这是我的代码

public class GMailOauthSender
{
    private Session session;

    public SMTPTransport connectToSmtp(String host, int port, String userEmail, String oauthToken, boolean debug) throws Exception {

        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");
        props.put("mail.smtp.sasl.enable", "false");
        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 transport;
    }

    public synchronized void sendMail(String subject, String body, String user, String oauthToken, String recipients) {
        try {
            SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com",587,user,oauthToken,true);

            MimeMessage message = new MimeMessage(session);
            DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
            message.setSender(new InternetAddress(user));   
            message.setSubject(subject);   
            message.setDataHandler(handler);   

            if (recipients.indexOf(',') > 0)   
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
            else  
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   

            smtpTransport.sendMessage(message, message.getAllRecipients());   
        } catch (Exception e) {
            Log.d("test", e.getMessage());
        }
    }
}

I have got this code from some where in stack overflow. 我从堆栈溢出的某些地方获取了此代码。 any help?? 有帮助吗?

The error you are receiving 您收到的错误

eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==

decodes to the following using a Base64 Decoder 使用Base64解码器解码为以下内容

{"status":"400","schemes":"Bearer","scope":"https://mail.google.com/"}

What this ends up meaning (as cryptic as the message is) is that the authentication token that you are using has expired. 最终的含义(与消息一样神秘)是您使用的身份验证令牌已过期。 You need to invalidate it and then get a new one (simply request the token again). 您需要使它无效,然后获得一个新的(只需再次请求令牌)。

You invalidate the token like this: 您可以这样使令牌无效:

mAccountManager.invalidateAuthToken("con.google", mAuthenticationToken);

I hope that this is helpful :) 我希望这会有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM