Sending Emails with SMTP in Delphi

Send emails through AhaSend’s SMTP servers using Delphi and the powerful Indy networking library. This guide covers everything from basic setup to advanced features like HTML content, attachments, and custom headers.
Delphi & Indy Power: Delphi’s robust development environment combined with Indy’s comprehensive networking components provides excellent email capabilities for desktop and server applications.
Special Thanks: This guide was partly provided by Kirk, one of our amazing customers! We’re grateful for the Delphi community’s contributions and expertise in helping fellow developers integrate with AhaSend.

Prerequisites

Before you begin, ensure you have:
Need SMTP Credentials? If you haven’t created SMTP credentials yet, check out our SMTP Credentials guide for step-by-step instructions.

Indy Components Overview

Delphi’s Indy library provides powerful networking components for email operations:

TIdSMTP

Purpose: Main SMTP client component
Features: Connection, auth, sending
Security: TLS/SSL support
Location: Internet tab in component palette

TIdMessage

Purpose: Email message construction
Features: Headers, body, attachments
Encoding: MIME support
Location: Internet tab in component palette

Connection Settings

Use these settings for all Delphi SMTP configurations with AhaSend:

Primary Server

Host: send.ahasend.com
Ports: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required

US Server

Host: send-us.ahasend.com
Ports: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required

Basic Email Examples

Component Setup

First, add the required Indy components to your form or data module:
// Add these components to your form/data module:
// - TIdSMTP (name: IdSMTP1)
// - TIdSSLIOHandlerSocketOpenSSL (name: IdSSL1)
// - TIdMessage (name: IdMessage1)
// - TIdEncoderMIME (name: IdEncoderMIME1)

// Required units in your uses clause:
uses
  IdSMTP, IdSSLIOHandlerSocketOpenSSL, IdMessage,
  IdEncoderMIME, IdGlobal, IdText, IdAttachmentFile;

Basic Examples

procedure TForm1.SendPlainTextEmail;
var
  IdMessage: TIdMessage;
begin
  // Configure SMTP component
  IdSMTP1.IOHandler := IdSSL1;
  (IdSMTP1.IOHandler as TIdSSLIOHandlerSocketOpenSSL).PassThrough := False;
  IdSMTP1.Username := 'YOUR_SMTP_USERNAME';
  IdSMTP1.Password := 'YOUR_SMTP_PASSWORD';
  IdSMTP1.Host := 'send.ahasend.com';
  IdSMTP1.Port := 587;
  IdSMTP1.AuthType := satNone;
  IdSMTP1.UseTLS := utUseExplicitTLS;
  IdSMTP1.UseEhlo := True;

  // Create message
  IdMessage := TIdMessage.Create(nil);
  try
    IdMessage.From.Address := '[email protected]';
    IdMessage.From.Name := 'Your Company';
    IdMessage.Recipients.Add.Address := '[email protected]';
    IdMessage.Subject := 'Welcome to our platform!';
    IdMessage.Body.Text := 'Thanks for signing up. We''re excited to have you!';
    IdMessage.ContentType := 'text/plain';

    // Send the email
    IdSMTP1.Connect;
    try
      if IdSMTP1.Authenticate then
      begin
        IdSMTP1.SendCmd('AUTH PLAIN ' + IdEncoderMIME1.EncodeString(#0 + IdSMTP1.Username + #0 + IdSMTP1.Password));
        IdSMTP1.Send(IdMessage);
        ShowMessage('Email sent successfully!');
      end;
    finally
      IdSMTP1.Disconnect;
    end;
  finally
    IdMessage.Free;
  end;
end;

Testing with Sandbox Mode

Use sandbox mode to safely test your Delphi email integration:
procedure TForm1.SendSandboxTest;
var
  IdMessage: TIdMessage;
begin
  // Configure SMTP component
  IdSMTP1.IOHandler := IdSSL1;
  (IdSMTP1.IOHandler as TIdSSLIOHandlerSocketOpenSSL).PassThrough := False;
  IdSMTP1.Username := 'YOUR_SMTP_USERNAME';
  IdSMTP1.Password := 'YOUR_SMTP_PASSWORD';
  IdSMTP1.Host := 'send.ahasend.com';
  IdSMTP1.Port := 587;
  IdSMTP1.AuthType := satNone;
  IdSMTP1.UseTLS := utUseExplicitTLS;
  IdSMTP1.UseEhlo := True;

  // Create message
  IdMessage := TIdMessage.Create(nil);
  try
    IdMessage.From.Address := '[email protected]';
    IdMessage.Recipients.Add.Address := '[email protected]';
    IdMessage.Subject := 'Sandbox Test Email';
    IdMessage.Body.Text := 'This email is sent in sandbox mode for testing.';

    // Enable sandbox mode
    IdMessage.ExtraHeaders.Values['AhaSend-Sandbox'] := 'true';
    IdMessage.ExtraHeaders.Values['AhaSend-Sandbox-Result'] := 'deliver';

    // Send the email
    IdSMTP1.Connect;
    try
      if IdSMTP1.Authenticate then
      begin
        IdSMTP1.SendCmd('AUTH PLAIN ' + IdEncoderMIME1.EncodeString(#0 + IdSMTP1.Username + #0 + IdSMTP1.Password));
        IdSMTP1.Send(IdMessage);
        ShowMessage('Sandbox email sent successfully!');
      end;
    finally
      IdSMTP1.Disconnect;
    end;
  finally
    IdMessage.Free;
  end;
end;
Sandbox Benefits: Emails sent in sandbox mode are free, trigger webhooks normally, and never actually deliver to recipients - perfect for development and testing.

Resources