【问题标题】:cannot send email using gmail account from android using javax无法使用 javax 从 android 的 gmail 帐户发送电子邮件
【发布时间】:2017-01-10 09:18:39
【问题描述】:

发送电子邮件时出错:

返回 451 4.5.0 SMTP protocol 违规,请参阅 RFC 2821 g5sm13340466pfg.0 - gsmtp.

得到oauth token后,键入和oauth从gmail过期。

以下代码用于测试使用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 也是“字符串”或“标题开头”字符

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 嗨,我正在使用 JavaMail,但它只显示如何通过 imap 连接阅读消息,我打算做的是通过 smtp 发送消息
    • 如果您点击链接,它会解释如何只需将属性名称中的“imap”替换为“smtp”。不清楚吗?
    • 如果您仍然感到困惑,这是您要使用的send method
    • 您好,发送方法需要密码,这是否意味着 oauth 令牌将放在密码部分?是的,我仍然很困惑
    • 嗨,可以使用您提供的链接发送,虽然密码参数是用户帐户的实际密码,但是当我与 Store.connect 的方法进行比较时,oauth 令牌是也放置在密码参数中,因此当我尝试在 Transport.send 函数的密码参数中传递 oauth 令牌时,它可以工作,并且我的邮件帐户收到了电子邮件。非常感谢。
    【解决方案2】:

    如果是邮寄的表格,那么你没有提到发送邮件的要求

    点击here

    或者如果你想直接使用php发送,那么

     <?php
     $to = $_POST['to'];
     $from = $_POST['from'];
     $subject = $_POST['subject'];
     $message = "From: ".$name."\r\n";\
     $headers = "From:" . $from;
     mail($to,$subject,$message,$headers);
     ?> 
    

    【讨论】:

    • 嗨,我不喜欢使用 3rd 方应用程序发送电子邮件,因为我们无法确定 3rd 方应用程序是否可用。这就是为什么我喜欢通过我的应用程序手动发送它。不,我正在使用 android java
    【解决方案3】:

    您是否尝试过在这里授予权限:

    https://www.google.com/settings/security/lesssecureapps

    这样的一些错误是因为你没有完整的属性,补充一下:

    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    

    【讨论】:

    • 您好,这是旧样式,将来可能不支持,使用 oauth2 您不需要打开 lesssecureapps,而且它更安全,您不能只让您的应用程序的用户允许不太安全的应用程序,它会使那里的电子邮件易受攻击
    猜你喜欢
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 2013-03-24
    • 2011-07-16
    • 2013-04-25
    • 2011-04-06
    • 1970-01-01
    • 2011-12-15
    相关资源
    最近更新 更多