Jump to content
JIMSMITH

sending email with oauth2

Recommended Posts

Has anybody been able to send email from Delphi using oauth2.  Every example that I have seen includes SMTP.UseTLS := utUseExplicitTLS; and SMTP.AuthType :=  satOAuth2; which appear undefined in my version 11 or 10.x?  I am starting from scratch as Google tends to change access requirements to send email.

Share this post


Link to post

With Indy is a little bit complex, you must install the github version and a branch "ssl-oauth". You must delete the original Indy shipped.

But now some works were done and I don't know how is the status of Indy repository and relative branch.

 

Alternative the is a thirdy tool not free (based on Indy): https://www.esegece.com/

 

You can try also ICS that is free, but I don't know if they have implemented Oauth protocol: https://wiki.overbyte.eu/wiki/index.php/ICS_Download

 

Bye

Edited by DelphiUdIT

Share this post


Link to post
14 hours ago, JIMSMITH said:

Has anybody been able to send email from Delphi using oauth2.

Yes.

14 hours ago, JIMSMITH said:

Every example that I have seen includes SMTP.UseTLS := utUseExplicitTLS; and SMTP.AuthType :=  satOAuth2; which appear undefined in my version 11 or 10.x?

There has never been an satOAuth2 option for the AuthType property.  The correct option is satSASL instead.  And then you can add a suitable TIdSASL-derived component to the TIdSMTP.SASLMechanisms collection.  But Indy's master code does not have a SASL component for OAuth yet...

12 hours ago, DelphiUdIT said:

With Indy is a little bit complex, you must install the github version and a branch "ssl-oauth". You must delete the original Indy shipped.

But now some works were done and I don't know how is the status of Indy repository and relative branch.

The status of that branch has not changed, I still need to merge it into the master branch.  It is on my TODO list.

 

In the meantime, it is possible to use the TIdSMTP.SendCmd() method to manually send the relevant XOATH2 command per Gmail's documentation (https://developers.google.com/gmail/imap/xoauth2-protocol), eg:

var creds: string;
creds := Format('user=%s'#1'auth=Bearer %s'#1#1, [Username, Token]);
IdSMTP1.SendCmd('AUTH XOATH2 ' + TIdEncoderMIME.EncodeString(creds), ['235']);

// Update the DidAuthenticate property so Send() won't try to authenticate again...
IdSMTP1.AuthType := satNone;
IdSMTP1.Authenticate;

Alternatively, you can use the sample code from this repo:

https://github.com/geoffsmith82/GmailAuthSMTP/

Edited by Remy Lebeau

Share this post


Link to post
8 hours ago, Remy Lebeau said:

Yes.

There has never been an satOAuth2 option for the AuthType property.  The correct option is satSASL instead.  And then you can add a suitable TIdSASL-derived component to the TIdSMTP.SASLMechanisms collection.  But Indy's master code does not have a SASL component for OAuth yet...

The status of that branch has not changed, I still need to merge it into the master branch.  It is on my TODO list.

 

In the meantime, it is possible to use the TIdSMTP.SendCmd() method to manually send the relevant XOATH2 command per Gmail's documentation (https://developers.google.com/gmail/imap/xoauth2-protocol), eg:


var creds: string;
creds := Format('user=%s'#1'auth=Bearer %s'#1#1, [Username, Token]);
IdSMTP1.SendCmd('AUTH XOATH2 ' + TIdEncoderMIME.EncodeString(creds), ['235']);

// Update the DidAuthenticate property so Send() won't try to authenticate again...
IdSMTP1.AuthType := satNone;
IdSMTP1.Authenticate;

Alternatively, you can use the sample code from this repo:

https://github.com/geoffsmith82/GmailAuthSMTP/
Thanks for responding to my message. The
code that you shared above with creds and ISMTP1, which full example does that get injected into?

Thanks for responding to my post.  Can you advise which example code should I place the creds and IdSMTP1 code into.

FYI:  The geoffsmith sample project prompts for user authentication.  I can have that as this will execute as a back end process and not be visible to the

ui.  I just need to send an email

 

Edited by JIMSMITH
missing

Share this post


Link to post
12 hours ago, Remy Lebeau said:

The status of that branch has not changed, I still need to merge it into the master branch.  It is on my TODO list.

I have since merged the sasl-oauth branch into the master code earlier today.

  • Like 1
  • Thanks 1

Share this post


Link to post
3 hours ago, JIMSMITH said:

Thanks for responding to my post.  Can you advise which example code should I place the creds and IdSMTP1 code into.

FYI:  The geoffsmith sample project prompts for user authentication.  I can have that as this will execute as a back end process and not be visible to the

ui.  I just need to send an email

It doesn't matter if the username and token come from a UI or a database or a config file or an automated process. It's all the same when it gets down to the SMTP level.

Share this post


Link to post
13 hours ago, Remy Lebeau said:

I have since merged the sasl-oauth branch into the master code earlier today.

I install the new Indy from github and the old PR299 (with the same changes like always to maintain compatibility with Embarcadero environment and third parties) and all is working with RAD 12.2 Patch 1.

I tried with the SSL DLL 3.1.5 for HTTPS and e-mail (not Oauth) and is working.

Also the new OAuths in the components palette. I didn't try them.

 

image.png.24f6fa2bb267adf5de72026f7dc2fa19.png

 

The new (but it worse) is that "LivePreview" should not be installed. It generate AV in the IDE on startup and shutdown.

 

Share this post


Link to post
21 hours ago, DelphiUdIT said:

I install the new Indy from github and the old PR299 (with the same changes like always to maintain compatibility with Embarcadero environment and third parties) and all is working with RAD 12.2 Patch 1.

That's good to hear, thanks.

 

Though, PR299 is closed now. There is new effort for OpenSSL 3x support in a new repo:

 

https://github.com/IndySockets/IndyTLS-OpenSSL

 

https://www.indyproject.org/2024/08/05/ongoing-work-in-indy-for-openssl-updates/

 

21 hours ago, DelphiUdIT said:

Also the new OAuths in the components palette. I didn't try them.

Oh good, glad to see they made it onto the palette ok. The icons were a last-minute addition.

21 hours ago, DelphiUdIT said:

The new (but it worse) is that "LivePreview" should not be installed. 

https://github.com/IndySockets/Indy/wiki/Updating-Indy#delphicbuilderradstudio-101-berlin-and-later

 

Share this post


Link to post
24 minutes ago, Remy Lebeau said:

Oh good, glad to see they made it onto the palette ok. The icons were a last-minute addition.

Ehmmm, I explain bad, it's your update. The Palette come from Indy source repo, already in the recenct past he was added (form Oauth-ssl branch ?).

 

All other points, I already know about them of course and I tried in the recent past the very first commit of TLS-OpenSSL with success. But I wanted to try the PR299 for historical test and to see if LivePreview was working like in the past (In the 11.x, 12, and 12.1 LivePreview was working with Indy Repo applied).

Edited by DelphiUdIT

Share this post


Link to post
13 minutes ago, DelphiUdIT said:

Ehmmm, I explain bad, it's your update. The Palette come from Indy source repo, already in the recenct past he was added (form Oauth-ssl branch ?).

Yes, the ssl-oauth branch was registering the components on the palette, but I did not have actual icons for them until right before I merged the branch.  I didn't have a chance to test the icons in the IDE.

Edited by Remy Lebeau

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×