Delphi 11 Indy GMail SMTP error "Username and Password not accepted"

Viewed 577

I'm using Delphi 11 for developing windows application and I'm using SMTP for sending mail and from last week I'm getting the error - "Username and Password not accepted". But, earlier the same code was working fine. Please provide some solution to fix this issue.

with IdSMTP1 Do
begin

  IOHandler := IdSSLIOHandlerSocketOpenSSL1;
  UseTLS    := utUseImplicitTLS;
  Host      := SMTP;
  Username  := FromUser;
  Password  := FromPassword;
  Port      := StrToIntDef(PortNumber, 0);

  IdMessage1.From.Address := IdSMTP1.Username; // sender
  IdMessage1.From.Name := 'Subject';

  try
    Connect;
    try
      Send(IdMessage1);
      Result := True;
    finally
      Disconnect;
    end;
  except
    on E:Exception do
    begin

    end;
  end;
end;
1 Answers

Google released the following announcement not too long ago:

Less secure apps & your Google Account

To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.

So, you have a few choices to continue using GMail:

  • Go into your Gmail account settings and turn back on the "Allow less secure apps" setting (Google turned it off automatically). This is not recommended, but is still supported (for now), and will not require any code changes.

  • Enable 2-Step Verification on your Gmail account, and then generate an App-Specific Password. Use that instead of your normal password in your FromPassword variable. No other code changes are needed. This is the preferred solution if you don't want to change your code.

  • Update your code to use OAuth authentication with TIdSMTP, per Google's documentation: OAuth 2.0 Mechanism. For instance, see this GitHub repo for a 3rd-party OAuth implementation that works with Indy's TIdSMTP.

    Also, I recently checked in my own sasl-oauth branch to Indy's GitHub repo containing some OAuth-related changes. You will still have to interface with Gmail yourself to obtain the necessary authentication token, but you can then use it with the new TIdSASLXOAuth2 class for the TIdSMTP.SASLMechanisms collection (note: it has not been tested yet).

  • re-write your code to use Gmail's own API instead of Indy.

Related