6

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;
hi hello
  • 61
  • 2
  • 2
    Gmail no longer supports basic authentication for SMTP. Please see, for example, [this link](https://en.delphipraxis.net/topic/1949-sending-email-via-gmail-using-oauth-20-via-indy/?tab=comments#comment-15113) – Dave Nottage Jun 13 '22 at 21:23
  • 1
    This question is basically the same as the one I asked last week: https://stackoverflow.com/questions/72554797/sending-email-via-gmail-with-app-specific-password – No'am Newman Jun 14 '22 at 04:45

1 Answers1

10

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Is this year old answer still up-to-date or has OAuth been implemented in Indy (and more specifically Rad Studio) now? – Mike Versteeg Jul 06 '23 at 15:31
  • 1
    @MikeVersteeg everything mentioned in this answer still applies today (except that `TIdSASLXOAuth2` has since been tested). The [`sasl-oauth`](https://github.com/IndySockets/Indy/tree/sasl-oauth) branch is still pending merge into Indy's main code, so it is not in RAD Studio yet. – Remy Lebeau Jul 06 '23 at 18:50