TIdSMTP Indy10, show me a "Connect timeout" just in Gmail and Windows 11

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

Certainly! Here's an example of how you can use TIdSMTP from the Indy library (Indy 10) in Delphi to demonstrate a "Connect timeout" when connecting to Gmail using Windows 11:

delphi
uses
IdSMTP, IdSSLOpenSSL;

procedure TestSMTPConnection;
var
SMTP: TIdSMTP;
begin
SMTP := TIdSMTP.Create(nil);
try
SMTP.Host := 'smtp.gmail.com';
SMTP.Port := 587;
SMTP.Username := 'your-email@gmail.com';
SMTP.Password := 'your-password';
SMTP.UseTLS := utUseExplicitTLS;
SMTP.ConnectTimeout := 5000; // Set the connection timeout to 5 seconds

try
SMTP.Connect;
try
// Connection successful
Writeln('Connected to SMTP server.');
finally
SMTP.Disconnect;
end;
except
on E: Exception do
begin
if E is EIdConnectTimeout then
Writeln('Connection timeout occurred.')
else
Writeln('Error connecting to SMTP server: ' + E.Message);
end;
end;
finally
SMTP.Free;
end;
end;

Make sure you have the Indy components (IdSMTP and IdSSLOpenSSL) properly installed in your Delphi IDE.

In this example, we create an instance of TIdSMTP and set the necessary properties such as Host, Port, Username, and Password for connecting to Gmail's SMTP server. We also enable TLS encryption using UseTLS property and set the connection timeout to 5 seconds (ConnectTimeout property).

Inside the try..except block, we attempt to connect to the SMTP server using SMTP.Connect. If the connection is successful, a message is displayed indicating the successful connection. If a connection timeout occurs, an EIdConnectTimeout exception is raised, and we handle it in the except block to display a specific message for the timeout scenario. Other exceptions are also caught and displayed.

Remember to replace 'your-email@gmail.com' with your actual Gmail email address and 'your-password' with your Gmail account password.

This code snippet demonstrates how to handle the "Connect timeout" scenario specifically when connecting to Gmail's SMTP server using TIdSMTP in Delphi with Windows 11.