无法使用javax从Android使用gmail帐户发送电子邮件

时间:2016-09-02 09:43:51

标签: java android oauth-2.0 javamail gmail-api

发送电子邮件时出错:

  

返回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());
        }

    }

设置有问题吗?

用户=%s \ 1auth = Bearer%s \ 1 \ 1中的\ 1是“字符串”或“标题开头”字符

3 个答案:

答案 0 :(得分:2)

你正在艰难地做这件事。 Let JavaMail do it for you.

答案 1 :(得分:0)

如果是通过邮件发送的表单,则您没有提到发送邮件的要求

点击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);
 ?> 

答案 2 :(得分:0)

你有没有尝试过这里的许可:

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");