TIdSMTP Gmail authentication failing with credentials error in C++Builder

I’m working with Indy components in C++Builder 11.3 for our RAD Server application. I need to send emails through Gmail SMTP but keep getting authentication errors.

When I try to connect, I get the “Username and Password not accepted” error with a reference to Google’s bad credentials page. I’m using the correct email and password, so I think this might be related to SSL/TLS configuration.

Here’s my current implementation:

void __fastcall TEmailForm::SubmitButtonClick(TObject *Sender)
{
    try
    {
        IdOpenSSLSetLibPath(L"C:\\ssl");

        TIdSMTP *SmtpClient = new TIdSMTP(NULL);
        SmtpClient->Host = "smtp.gmail.com";
        SmtpClient->Port = 587;
        SmtpClient->Username = "[email protected]";
        SmtpClient->Password = "app_password";
        SmtpClient->AuthType = satDefault;

        TIdSSLIOHandlerSocketOpenSSL *SslHandler = new TIdSSLIOHandlerSocketOpenSSL(SmtpClient);
        SslHandler->SSLOptions->SSLVersions << sslvTLSv1 << sslvTLSv1_1 << sslvTLSv1_2;
        SslHandler->SSLOptions->Mode = sslmUnassigned;
        SslHandler->SSLOptions->VerifyMode = TIdSSLVerifyModeSet();
        SslHandler->SSLOptions->VerifyDepth = 0;
        SmtpClient->IOHandler = SslHandler;

        SmtpClient->UseTLS = utUseExplicitTLS;

        TIdMessage *EmailMsg = new TIdMessage(NULL);
        EmailMsg->From->Address = "[email protected]";
        EmailMsg->Recipients->Add()->Address = "[email protected]";
        EmailMsg->Subject = "Test Message";
        EmailMsg->Body->Text = "Testing email functionality...";

        try {
            SmtpClient->Connect();
            SmtpClient->Send(EmailMsg);
            SmtpClient->Disconnect();
        } catch (const Exception &e) {
            LogMemo->Lines->Add("Connection Error: " + e.Message);
        }

        delete SmtpClient;
        delete SslHandler;
        delete EmailMsg;
    }
    catch (const Exception &ex)
    {
        LogMemo->Lines->Add("General Error: " + ex.Message);
    }
}

I’ve tried different SSL library versions and checked my credentials multiple times. Could this be a TLS version compatibility issue with Gmail’s servers? Any suggestions for the correct SSL setup for 64-bit Windows would be helpful.

I encountered similar issues when trying to authenticate with Gmail’s SMTP in C++Builder. It’s crucial to ensure that you’re using an App Password instead of your regular account password, as Google now requires this for third-party applications. You can generate one from your Google account under Security settings. Additionally, I recommend setting the SSLOptions->Mode to sslmClient instead of sslmUnassigned for a smoother connection. Changing the AuthType to satSASL can also help, as Gmail prefers OAuth for authentication. Lastly, verify that your OpenSSL libraries are up-to-date and compatible with 64-bit systems, as outdated versions can lead to further authentication issues.

Your SSL handler setup is the problem. Don’t set the manual SSL library path - let Indy handle it automatically. You’re creating the SSL handler with SmtpClient as owner, then assigning it to IOHandler. That creates memory management conflicts. Use NULL owner instead.

Gmail’s been tightening security lately. Make sure you generated your app password recently - they invalidate old ones. I got it working with satSASL authentication and setting the SSL handler’s PassThrough property to false before connecting. If you’re on a business account, check that less secure app access isn’t disabled in your Google workspace settings.

Check if 2-factor auth is enabled on your Gmail account - that usually breaks SMTP connections even with app passwords. Also try port 465 with utUseImplicitTLS instead of 587. Works better in my experience with CBuilder.