Obtain OAuth Credentials for my own GMail accountjavamail error: must issue starttls command firstJavaMail with Gmail: 535-5.7.1 Username and Password not acceptedsending mail to any website using java servletHow to use gmail OAUTH2 with older javamail versions in Android applicationImplementing sample code for authenticating to Gmail with OAuth2Android JavaMail IMAP store does not connect to Gmail with OAuth2.0 authenticationcom.sun.mail.smtp.SMTPSendFailedException JavamailJava Mail: Setting Chinese EncodingJavamail gmail and OAuth2Apache Common Mails: Exception: Sending the email to the following server failed : smtp.gmail.com:587

Owner keeps cutting corners and poaching workers for his other company

Leaving the USA for 10 yrs when you have asylum

Friend is very nitpicky about side comments I don't intend to be taken too seriously

is it possible to change a material depending on whether it is intersecting with another object?

How can faith be maintained in a world of living gods?

How to improvise or make pot grip / pot handle

Explaining "向けてじゃないよ"

Was Robin Hood's point of view ethically sound?

Is every sentence we write or utter either true or false?

How would two worlds first establish an exchange rate between their currencies

Do you need to burn fuel between gravity assists?

What is the delta-v required to get a mass in Earth orbit into the sun using a SINGLE transfer?

Can you pop microwave popcorn on a stove?

Is future tense in English really a myth?

Why does 8 bit truecolor use only 2 bits for blue?

More than three domains hosted on the same IP address

Can multiple public keys lead to the same shared secret in x25519?

Why did Tony's Arc Reactor do this?

Extra arrow heads appearing tikz

I multiply the source, you (probably) multiply the output!

Why can't some airports handle heavy aircraft while others do it easily (same runway length)?

How do we create our own symbolisms?

A PEMDAS issue request for explanation

Strategies for dealing with chess burnout?



Obtain OAuth Credentials for my own GMail account


javamail error: must issue starttls command firstJavaMail with Gmail: 535-5.7.1 Username and Password not acceptedsending mail to any website using java servletHow to use gmail OAUTH2 with older javamail versions in Android applicationImplementing sample code for authenticating to Gmail with OAuth2Android JavaMail IMAP store does not connect to Gmail with OAuth2.0 authenticationcom.sun.mail.smtp.SMTPSendFailedException JavamailJava Mail: Setting Chinese EncodingJavamail gmail and OAuth2Apache Common Mails: Exception: Sending the email to the following server failed : smtp.gmail.com:587






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I was using the older JavaMail API to send email from my Android app using password based authentication. The thing is that I am using my own GMail account for sending email (thus storing email/password in code - I know that is not a good idea)



new PasswordAuthentication(_user, _pass)



I have now upgraded to the newer version of JavaMail and I want to use OAuth2



implementation 'com.sun.mail:android-mail:1.6.2'
implementation 'com.sun.mail:android-activation:1.6.2'


How do I generate OAuth credentials for my own GMail account and use them to send email?




EDIT:



This is what I tried so far:



  1. Created a project on Google Console. From the Credentials screen, create OAuth Client ID - choose "Other". This gives me Client ID and Client Secret

  2. Using oauth2.py, used the Client ID and Client Secret to generate an Access Token and a Refresh Token (In the production app, I intend to hard code all 4: client id, secret, access, refresh token and continue using them "forever" for generating new access tokens)

  3. Tested that SMTP Authentication works using oauth2.py


  4. Modified my Java code for sending the mail as follows:



    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.debug", "true");
    props.put("mail.smtp.port", 587);
    props.setProperty("mail.smtp.socketFactory.port", 587);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth.mechanisms", "XOAUTH2");

    Session session = Session.getInstance(props);
    SMTPTransport transport = new SMTPTransport(session, null);
    transport.connect("smtp.gmail.com", _user, _accessToken);
    transport.sendMessage(msg, msg.getAllRecipients());


And now I am getting:
java.net.UnknownHostException: mail.google.com Unknown SMTP host: smtp.gmail.com



I am having trouble finding how to set the Access Token on the request. Instead of



transport.connect("smtp.gmail.com", _user, _accessToken);



I have also tried



String auth = new String(BASE64EncoderStream.encode(String.format("user=%s1auth=Bearer %s11", _user, _accessToken).getBytes()));
transport.issueCommand("AUTH XOAUTH2 " + auth, 235);


But same error.










share|improve this question


























  • See the JavaMail for Android page for help, it contains links to documents you'll need to read.

    – Bill Shannon
    Mar 28 at 15:36











  • Already looked at that documentation - could not find a way to generate access token for my own GMail account.

    – Bonton255
    Mar 28 at 20:17











  • It worked for me when I tried it many months ago. What exactly did you try? There's a web page to manage all your OAuth credentials at Google, did you find it?

    – Bill Shannon
    Mar 29 at 3:32











  • @BillShannon: To clarify, did you try logging in using your own GMail account, or allowing users to login using "their" accounts to send the mail? I can easily find the documentation for the latter. If you can, please share the link for the web page you mention?

    – Bonton255
    Mar 29 at 4:27











  • The JavaMail Android page links to the JavaMail OAuth2 page, which links to the Google OAuth2 instructions and the Google Developers Console. On that page select Credentials and you can manage your OAuth2 credentials that allow access to your account. Since you read all those pages and tried this already, tell us more exactly what you did and what happened.

    – Bill Shannon
    Mar 29 at 19:07

















1















I was using the older JavaMail API to send email from my Android app using password based authentication. The thing is that I am using my own GMail account for sending email (thus storing email/password in code - I know that is not a good idea)



new PasswordAuthentication(_user, _pass)



I have now upgraded to the newer version of JavaMail and I want to use OAuth2



implementation 'com.sun.mail:android-mail:1.6.2'
implementation 'com.sun.mail:android-activation:1.6.2'


How do I generate OAuth credentials for my own GMail account and use them to send email?




EDIT:



This is what I tried so far:



  1. Created a project on Google Console. From the Credentials screen, create OAuth Client ID - choose "Other". This gives me Client ID and Client Secret

  2. Using oauth2.py, used the Client ID and Client Secret to generate an Access Token and a Refresh Token (In the production app, I intend to hard code all 4: client id, secret, access, refresh token and continue using them "forever" for generating new access tokens)

  3. Tested that SMTP Authentication works using oauth2.py


  4. Modified my Java code for sending the mail as follows:



    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.debug", "true");
    props.put("mail.smtp.port", 587);
    props.setProperty("mail.smtp.socketFactory.port", 587);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth.mechanisms", "XOAUTH2");

    Session session = Session.getInstance(props);
    SMTPTransport transport = new SMTPTransport(session, null);
    transport.connect("smtp.gmail.com", _user, _accessToken);
    transport.sendMessage(msg, msg.getAllRecipients());


And now I am getting:
java.net.UnknownHostException: mail.google.com Unknown SMTP host: smtp.gmail.com



I am having trouble finding how to set the Access Token on the request. Instead of



transport.connect("smtp.gmail.com", _user, _accessToken);



I have also tried



String auth = new String(BASE64EncoderStream.encode(String.format("user=%s1auth=Bearer %s11", _user, _accessToken).getBytes()));
transport.issueCommand("AUTH XOAUTH2 " + auth, 235);


But same error.










share|improve this question


























  • See the JavaMail for Android page for help, it contains links to documents you'll need to read.

    – Bill Shannon
    Mar 28 at 15:36











  • Already looked at that documentation - could not find a way to generate access token for my own GMail account.

    – Bonton255
    Mar 28 at 20:17











  • It worked for me when I tried it many months ago. What exactly did you try? There's a web page to manage all your OAuth credentials at Google, did you find it?

    – Bill Shannon
    Mar 29 at 3:32











  • @BillShannon: To clarify, did you try logging in using your own GMail account, or allowing users to login using "their" accounts to send the mail? I can easily find the documentation for the latter. If you can, please share the link for the web page you mention?

    – Bonton255
    Mar 29 at 4:27











  • The JavaMail Android page links to the JavaMail OAuth2 page, which links to the Google OAuth2 instructions and the Google Developers Console. On that page select Credentials and you can manage your OAuth2 credentials that allow access to your account. Since you read all those pages and tried this already, tell us more exactly what you did and what happened.

    – Bill Shannon
    Mar 29 at 19:07













1












1








1








I was using the older JavaMail API to send email from my Android app using password based authentication. The thing is that I am using my own GMail account for sending email (thus storing email/password in code - I know that is not a good idea)



new PasswordAuthentication(_user, _pass)



I have now upgraded to the newer version of JavaMail and I want to use OAuth2



implementation 'com.sun.mail:android-mail:1.6.2'
implementation 'com.sun.mail:android-activation:1.6.2'


How do I generate OAuth credentials for my own GMail account and use them to send email?




EDIT:



This is what I tried so far:



  1. Created a project on Google Console. From the Credentials screen, create OAuth Client ID - choose "Other". This gives me Client ID and Client Secret

  2. Using oauth2.py, used the Client ID and Client Secret to generate an Access Token and a Refresh Token (In the production app, I intend to hard code all 4: client id, secret, access, refresh token and continue using them "forever" for generating new access tokens)

  3. Tested that SMTP Authentication works using oauth2.py


  4. Modified my Java code for sending the mail as follows:



    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.debug", "true");
    props.put("mail.smtp.port", 587);
    props.setProperty("mail.smtp.socketFactory.port", 587);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth.mechanisms", "XOAUTH2");

    Session session = Session.getInstance(props);
    SMTPTransport transport = new SMTPTransport(session, null);
    transport.connect("smtp.gmail.com", _user, _accessToken);
    transport.sendMessage(msg, msg.getAllRecipients());


And now I am getting:
java.net.UnknownHostException: mail.google.com Unknown SMTP host: smtp.gmail.com



I am having trouble finding how to set the Access Token on the request. Instead of



transport.connect("smtp.gmail.com", _user, _accessToken);



I have also tried



String auth = new String(BASE64EncoderStream.encode(String.format("user=%s1auth=Bearer %s11", _user, _accessToken).getBytes()));
transport.issueCommand("AUTH XOAUTH2 " + auth, 235);


But same error.










share|improve this question
















I was using the older JavaMail API to send email from my Android app using password based authentication. The thing is that I am using my own GMail account for sending email (thus storing email/password in code - I know that is not a good idea)



new PasswordAuthentication(_user, _pass)



I have now upgraded to the newer version of JavaMail and I want to use OAuth2



implementation 'com.sun.mail:android-mail:1.6.2'
implementation 'com.sun.mail:android-activation:1.6.2'


How do I generate OAuth credentials for my own GMail account and use them to send email?




EDIT:



This is what I tried so far:



  1. Created a project on Google Console. From the Credentials screen, create OAuth Client ID - choose "Other". This gives me Client ID and Client Secret

  2. Using oauth2.py, used the Client ID and Client Secret to generate an Access Token and a Refresh Token (In the production app, I intend to hard code all 4: client id, secret, access, refresh token and continue using them "forever" for generating new access tokens)

  3. Tested that SMTP Authentication works using oauth2.py


  4. Modified my Java code for sending the mail as follows:



    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.debug", "true");
    props.put("mail.smtp.port", 587);
    props.setProperty("mail.smtp.socketFactory.port", 587);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth.mechanisms", "XOAUTH2");

    Session session = Session.getInstance(props);
    SMTPTransport transport = new SMTPTransport(session, null);
    transport.connect("smtp.gmail.com", _user, _accessToken);
    transport.sendMessage(msg, msg.getAllRecipients());


And now I am getting:
java.net.UnknownHostException: mail.google.com Unknown SMTP host: smtp.gmail.com



I am having trouble finding how to set the Access Token on the request. Instead of



transport.connect("smtp.gmail.com", _user, _accessToken);



I have also tried



String auth = new String(BASE64EncoderStream.encode(String.format("user=%s1auth=Bearer %s11", _user, _accessToken).getBytes()));
transport.issueCommand("AUTH XOAUTH2 " + auth, 235);


But same error.







android javamail






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 22:27







Bonton255

















asked Mar 28 at 7:08









Bonton255Bonton255

1,0652 gold badges13 silver badges33 bronze badges




1,0652 gold badges13 silver badges33 bronze badges















  • See the JavaMail for Android page for help, it contains links to documents you'll need to read.

    – Bill Shannon
    Mar 28 at 15:36











  • Already looked at that documentation - could not find a way to generate access token for my own GMail account.

    – Bonton255
    Mar 28 at 20:17











  • It worked for me when I tried it many months ago. What exactly did you try? There's a web page to manage all your OAuth credentials at Google, did you find it?

    – Bill Shannon
    Mar 29 at 3:32











  • @BillShannon: To clarify, did you try logging in using your own GMail account, or allowing users to login using "their" accounts to send the mail? I can easily find the documentation for the latter. If you can, please share the link for the web page you mention?

    – Bonton255
    Mar 29 at 4:27











  • The JavaMail Android page links to the JavaMail OAuth2 page, which links to the Google OAuth2 instructions and the Google Developers Console. On that page select Credentials and you can manage your OAuth2 credentials that allow access to your account. Since you read all those pages and tried this already, tell us more exactly what you did and what happened.

    – Bill Shannon
    Mar 29 at 19:07

















  • See the JavaMail for Android page for help, it contains links to documents you'll need to read.

    – Bill Shannon
    Mar 28 at 15:36











  • Already looked at that documentation - could not find a way to generate access token for my own GMail account.

    – Bonton255
    Mar 28 at 20:17











  • It worked for me when I tried it many months ago. What exactly did you try? There's a web page to manage all your OAuth credentials at Google, did you find it?

    – Bill Shannon
    Mar 29 at 3:32











  • @BillShannon: To clarify, did you try logging in using your own GMail account, or allowing users to login using "their" accounts to send the mail? I can easily find the documentation for the latter. If you can, please share the link for the web page you mention?

    – Bonton255
    Mar 29 at 4:27











  • The JavaMail Android page links to the JavaMail OAuth2 page, which links to the Google OAuth2 instructions and the Google Developers Console. On that page select Credentials and you can manage your OAuth2 credentials that allow access to your account. Since you read all those pages and tried this already, tell us more exactly what you did and what happened.

    – Bill Shannon
    Mar 29 at 19:07
















See the JavaMail for Android page for help, it contains links to documents you'll need to read.

– Bill Shannon
Mar 28 at 15:36





See the JavaMail for Android page for help, it contains links to documents you'll need to read.

– Bill Shannon
Mar 28 at 15:36













Already looked at that documentation - could not find a way to generate access token for my own GMail account.

– Bonton255
Mar 28 at 20:17





Already looked at that documentation - could not find a way to generate access token for my own GMail account.

– Bonton255
Mar 28 at 20:17













It worked for me when I tried it many months ago. What exactly did you try? There's a web page to manage all your OAuth credentials at Google, did you find it?

– Bill Shannon
Mar 29 at 3:32





It worked for me when I tried it many months ago. What exactly did you try? There's a web page to manage all your OAuth credentials at Google, did you find it?

– Bill Shannon
Mar 29 at 3:32













@BillShannon: To clarify, did you try logging in using your own GMail account, or allowing users to login using "their" accounts to send the mail? I can easily find the documentation for the latter. If you can, please share the link for the web page you mention?

– Bonton255
Mar 29 at 4:27





@BillShannon: To clarify, did you try logging in using your own GMail account, or allowing users to login using "their" accounts to send the mail? I can easily find the documentation for the latter. If you can, please share the link for the web page you mention?

– Bonton255
Mar 29 at 4:27













The JavaMail Android page links to the JavaMail OAuth2 page, which links to the Google OAuth2 instructions and the Google Developers Console. On that page select Credentials and you can manage your OAuth2 credentials that allow access to your account. Since you read all those pages and tried this already, tell us more exactly what you did and what happened.

– Bill Shannon
Mar 29 at 19:07





The JavaMail Android page links to the JavaMail OAuth2 page, which links to the Google OAuth2 instructions and the Google Developers Console. On that page select Credentials and you can manage your OAuth2 credentials that allow access to your account. Since you read all those pages and tried this already, tell us more exactly what you did and what happened.

– Bill Shannon
Mar 29 at 19:07












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391932%2fobtain-oauth-credentials-for-my-own-gmail-account%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391932%2fobtain-oauth-credentials-for-my-own-gmail-account%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript