java - 无法使用javax从android使用gmail帐户发送电子邮件

标签 java android oauth-2.0 jakarta-mail gmail-api

发送电子邮件时出错:

returns 451 4.5.0 SMTP protocol violation, see RFC 2821 g5sm13340466pfg.0 - gsmtp.

获取 oauth token 后,输入 gmail 的 oauth 过期信息。

以下代码用于测试使用 oauth2 发送电子邮件。

private String GenerateOAuth2String(boolean base64_encode){
        String OAuthString = "";
        Log.e("SendTestActivity", "AuthToken: " + authToken);
        OAuthString = String.format("user=%s\1auth=Bearer %s\1\1", userName, authToken);
        Log.e("SendTestActivity", "non base 64: " + OAuthString);
        if (base64_encode)
            OAuthString = Base64.encodeToString(OAuthString.getBytes(), Base64.DEFAULT);
        Log.e("SendTestActivity", "base 64: " + OAuthString);
        return OAuthString;
    }

    private synchronized void sendMail(String subject, String body, String user, String recipients) {
        try {           
            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(true);

            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("smtp.gmail.com", 587, user, emptyPassword);

            transport.issueCommand("AUTH XOAUTH2 " + GenerateOAuth2String(true),
                    235);

            MimeMessage message = new MimeMessage(session);

            // Set From: header field of the header.
             message.setFrom(new InternetAddress(user));

             // Set To: header field of the header.
             message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients));

             // Set Subject: header field
             message.setSubject(subject);

             // Now set the actual message
             message.setText(body);

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

            Transport.send(message);

            Log.e("SendTestActivity", "email sent");

        } catch (Exception e) {
            Log.e("SendTestActivity", e.getMessage());
        }

    }

设置有问题吗?

user=%s\1auth=Bearer %s\1\1 中的\1 也是“字符串”或“标题开始”字符

最佳答案

你做得很艰难。 Let JavaMail do it for you.

关于java - 无法使用javax从android使用gmail帐户发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39289316/

相关文章:

java - Android WebView仅显示在半屏上

java - 使用 session 加载图像名称

android - takePicture() 横向模式旋转android

java - 并行列表与具有多个变量的类列表

代表流程的 azure 广告范围始终重新审视所有同意的范围

asp.net-mvc - ASP.NET OWIN 使用 Microsoft Live ID 登录 - 无效请求和 Google 重定向 URL 不匹配

java - Selenium 中所有页面的行数

java - 在 Java 中实现 WebSocket

android - 我怎样才能得到我在android中知道其包名的其他应用程序的uid?

Swift Vapor unsupported_grant_type 无效的签名/OAuth 访问 token