Implementing OAuth 2.0 for Gmail integration with Indy

I’m trying to send emails through Gmail using Indy in my app. Right now, I have to tell users to allow less secure apps in their Google account settings. This isn’t great for security.

I want to use OAuth 2.0 instead, but I’m not sure how to add it to my Indy code. Can anyone help me update my current setup to use OAuth 2.0? This way, users won’t have to lower their security settings.

Here’s a simplified version of what I’m working with now:

procedure SendEmail;
var
  SMTP: TIdSMTP;
  Msg: TIdMessage;
  SSL: TIdSSLIOHandlerSocketOpenSSL;
begin
  SMTP := TIdSMTP.Create(nil);
  Msg := TIdMessage.Create(nil);
  SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  try
    // Set up message details
    Msg.From.Address := '[email protected]';
    Msg.Recipients.Add.Address := '[email protected]';
    Msg.Subject := 'Test Email';
    Msg.Body.Text := 'This is a test email sent using Indy.';

    // Configure SSL
    SSL.SSLOptions.Method := sslvTLSv1;
    SMTP.IOHandler := SSL;
    SMTP.UseTLS := utUseExplicitTLS;

    // Set up SMTP details
    SMTP.Host := 'smtp.gmail.com';
    SMTP.Port := 587;
    SMTP.Username := '[email protected]';
    SMTP.Password := 'your_password';

    // Send the email
    SMTP.Connect;
    try
      SMTP.Send(Msg);
    finally
      SMTP.Disconnect;
    end;
  finally
    SMTP.Free;
    Msg.Free;
    SSL.Free;
  end;
end;

How can I modify this to use OAuth 2.0 instead of a regular password? Thanks for any help!

hey scarlettturner, oauth 2.0 with indy can be tricky. you’ll need to get an access token from google first. then, replace the username/password lines with something like:

SMTP.AuthType := satSASL;
SMTP.SASLMechanisms.Add.SASL.Create(TIdSASLXOAuth2);
SMTP.SASLMechanisms[0].Username := ‘[email protected]’;
SMTP.SASLMechanisms[0].Password := ‘your_access_token’;

hope this helps! let me know if u need more info

I’ve been down this road before, and it’s definitely a step in the right direction for security. Implementing OAuth 2.0 with Indy for Gmail integration is a bit of a process, but it’s worth it.

First, you’ll need to set up your project in the Google Developer Console and get your client ID and secret. Then, you’ll need to implement the OAuth 2.0 flow to get an access token.

Once you have the token, you can modify your existing code. Add the TIdSASLXOAuth2 unit to your uses clause, and replace the username/password setup with something like this:

SMTP.AuthType := satSASL;
var SASL: TIdSASLListEntry;
SASL := SMTP.SASLMechanisms.Add;
SASL.SASL := TIdSASLXOAuth2.Create(nil);
TIdSASLXOAuth2(SASL.SASL).Token := ‘your_access_token’;

Remember to handle token refresh as they expire. It’s a bit more complex, but your users will appreciate the added security.

Implementing OAuth 2.0 with Indy requires several modifications to your application. Begin by setting up a project in the Google Cloud Console and obtaining the necessary credentials.

Instead of using a simple username and password, you should include the TIdSASLXOAuth2 unit in your code and follow Google’s OAuth 2.0 flow to retrieve an access token.

Adjust your SMTP configuration to use SASL with XOAUTH2, and ensure your implementation handles token expiration and refresh appropriately for secure email integration.