Networking and Internet Protocols
Indy (Internet Direct) is bundled. It's a blocking-socket library with clients and servers for TCP, UDP, HTTP(S), SMTP, POP3, IMAP4, FTP, Telnet, SNTP, ICMP (ping), and more. HTTPS/SMTP over TLS requires the OpenSSL DLLs (check version compatibility: Indy's newer OpenSSL support may need updates).
Simple TCP server with Indy:
Alternatives: System.Net.Socket (TSocket, cross-platform), ICS (Internet Component Suite) by François Piette (async, event-driven), Synapse, sgcWebSockets (WebSockets, MQTT, HTTP/2), mORMot networking, TNetHTTPClient for HTTP, WebSocket components, MQTT clients (TMS MQTT, sgc).
uses IdSMTP, IdMessage, IdSSLOpenSSL, IdExplicitTLSClientServerBase;
var SMTP := TIdSMTP.Create(nil);
var Msg := TIdMessage.Create(nil);
var SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
SSL.SSLOptions.Method := sslvTLSv1_2;
SMTP.IOHandler := SSL;
SMTP.UseTLS := utUseExplicitTLS;
SMTP.Host := 'smtp.example.com'; SMTP.Port := 587;
SMTP.Username := 'user'; SMTP.Password := 'secret';
Msg.From.Address := '[email protected]';
Msg.Recipients.EMailAddresses := '[email protected]';
Msg.Subject := 'Report';
Msg.Body.Text := 'Hello from Delphi';
SMTP.Connect;
try SMTP.Send(Msg); finally SMTP.Disconnect; end;
finally
SSL.Free; Msg.Free; SMTP.Free;
end;procedure TForm1.IdTCPServer1Execute(AContext: TIdContext); // runs in a separate thread per connection
begin
var Line := AContext.Connection.IOHandler.ReadLn;
AContext.Connection.IOHandler.WriteLn('Echo: ' + Line);
end;