2

Error sending email:

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

After getting the oauth token, type and oauth expire from gmail.

here the following code used to test send email using 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());
        }

    }

is there something wrong with the setup?

also does the \1 in user=%s\1auth=Bearer %s\1\1 a "string" or a "start of heading" character

2 Answers 2

2

You're doing it the hard way. Let JavaMail do it for you.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi, I am using JavaMail, but it only shows how to connect to read messages through imap, what i am intending to do is send messages through smtp
If you follow the link, it explains how you only have to replace "imap" with "smtp" in the property names. Was that not clear?
In case you're still confused, this is the send method you want to use.
hi the send method requires the password does it mean that the oauth token will be placed in the password section? and yes I am still confused
hi, was able to send using the link you gave, I though the password parameter is for the actual password of the user's account, but then when I compare to the Store.connect's method the oauth token is also placed in the password parameter so when I tried to pass the oauth token in the password parameter of Transport.send function it works and my mail account the received the email. thanks very much.
|
0

Did you tried giving permission here:

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

Some of the errors like that are because you don't have the complete properties, complements with this:

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

1 Comment

Hi, this is old style and may not be supported in the future, using oauth2 you are not required to turn on lesssecureapps, plus it is more secure, you cannot just let users of your app to allow less secure app, it will make there email vulnerable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.