Jump to content
Ugochukwu Mmaduekwe

Sending Email via GMail Using OAuth 2.0 via Indy

Recommended Posts

1 hour ago, J23 said:

Thank you very much for your quick reply. Unfortunately, there are many problems:

 

1. {$HPPEMIT LINKUNIT}    - [DCC Error] U_DCS_OAuth2.pas(23): E1030 Invalid compiler directive: 'HPPEMIT'

and uses:

2.   Data.Bind.ObjectScope, - [DCC Fatal Error] U_DCS_OAuth2.pas(31): F1026 File not found: 'Data.Bind.ObjectScope.dcu'

...

3. REST.Client,                     - [DCC Fatal Error] U_DCS_OAuth2.pas(33): F1026 File not found: 'REST.Client.dcu'

and so on.

 

J23

 

Yeah...

 

It appears Delphi 2010 does not have the REST components...

 

Maybe you should try @Remy Lebeau suggestion, use the latest Indy  branch.

 

Good luck!

 

 

Share this post


Link to post

Thank you very much for your quick reply.

12 hours ago, Remy Lebeau said:
15 hours ago, J23 said:


i tried to run @Geoffrey Smith gmailauthsmtp in my Delphi 2010 but failed.

Failed how, exactly?

I've corrected dotted unit names but I don't have REST.Authenticator.OAuth module.

As I wrote before I've tried to install "MARS-master" REST library with no success.

 

12 hours ago, Remy Lebeau said:

You can either turn on 2-Factor Authentication

In fact my problem is with Office365 mail, but I think is similar to Gmail.

I've tried in my home Office365 trial account App-Specific password erlier and it works very well. But my University net admin did not allow 2-Factor Authentication.

12 hours ago, Remy Lebeau said:

install the latest Indy OAuth2 branch code..... and then you can use TIdHTTP to retrieve an access token from Google, and use TIdSASLXOAuth2 to use that token with TIdSMTP/TIdPOP3/TIdIMAP4.

Yesterday I downloaded and installed the latest INDY package, but I didn't notice this version of Oath2. I've installed it today.

I don't know if I can handle the next steps you described. I'm afraid this may not be easy.

I will try.

Thank you again,

J23

Share this post


Link to post
15 hours ago, J23 said:

1. {$HPPEMIT LINKUNIT}    - [DCC Error] U_DCS_OAuth2.pas(23): E1030 Invalid compiler directive: 'HPPEMIT'

That directive was introduced in XE5.

Share this post


Link to post
13 hours ago, J23 said:

Yesterday I downloaded and installed the latest INDY package, but I didn't notice this version of Oath2. I've installed it today.

I don't know if I can handle the next steps you described. I'm afraid this may not be easy.

I will try.

What exactly are you having trouble with?

Share this post


Link to post
12 hours ago, Remy Lebeau said:

What exactly are you having trouble with?

I do not know yet. The problem will certainly appear right after the start of programming ;-). I need to start by reading the documentation. An example that works even in the worst way guarantees success.

 

Share this post


Link to post
On 11/23/2022 at 12:45 AM, Remy Lebeau said:

and then you can use TIdHTTP to retrieve an access token from Google, and use TIdSASLXOAuth2 to use that token with TIdSMTP/TIdPOP3/TIdIMAP4

Good morning again. After an unplanned break, I'm back to the problem. I tried to get the token as you advised using TIdHTTP but I don't know how to do it without REST libraries (Delphi 2010). Finally, I installed ICS (overbyte.be) and with the help of the RestOAuth component I was able to get the token. I still don't know what to do next: how to use the token, IdSASLXOAuth21 and IdSMTP to send an email.

J23

Share this post


Link to post
3 hours ago, J23 said:

I tried to get the token as you advised using TIdHTTP but I don't know how to do it without REST libraries (Delphi 2010).

REST is just standard HTTP, usually with JSON data.  Indy doesn't have any REST-specific components, but pretty much anything you can with other REST components can also be done manually with TIdHTTP.

3 hours ago, J23 said:

I still don't know what to do next: how to use the token, IdSASLXOAuth21 and IdSMTP to send an email.

You use the token pretty much the same way you use any other SASL credentials with TIdSMTP:

  • Add TIdSASLXOAuth2 to the TIdSMTP.SASLMechanisms collection, and set the TIdSMTP.AuthType property to satSASL.
  • Assign a TIdUserPassProvider to the TIdSASLXOAuth2.UserPassProvider property, and then assign the retrieved token to the TIdUserPassProvider.Password property.
    • Alternatively, use the TIdSASLXOAuth2.OnGetAccessToken event to retrieve the token on-demand.
  • Use TIdMessage and TIdSMTP as needed (fill out email, configure Host/Port, UseTLS, etc, and then call TIdSMTP.Connect(), TIdSMTP.Send(), TIdSMTP.Disconnect(), etc).

 

 

  • Like 1

Share this post


Link to post
On 2/8/2023 at 9:14 PM, Remy Lebeau said:

You use the token pretty much the same way you use any other SASL credentials with TIdSMTP:

It works, OAuth2 mail sent and received!
On 2/8/2023 at 9:14 PM, Remy Lebeau said:

can also be done manually with TIdHTTP

Thank you very much Remy.
Now maybe I'll try to get a token using IdHTTP ;-)
Regards,
J23

Share this post


Link to post
7 hours ago, fhaut said:

https://github.com/geoffsmith82/GmailAuthSMTP

Has this example changed so that it will now work in Delphi 2010? I have downloaded it and IMHO the Rest libraries are still needed.

Share this post


Link to post
On 2/8/2023 at 9:14 PM, Remy Lebeau said:

pretty much anything you can with other REST components can also be done manually with TIdHTTP

I tried and failed. I think I have the HTTPrequest correct: pasted directly into the browser it brings up the login window. After user accept, the browser reports the inability to connect to the localhost server. How to call default browser and receive response to get AuthCode? Of course without using REST.

Once I get the AuthCode, I'll call IdHHTP.Post method and get the Token - that shouldn't be a problem.

J23

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
×