Send Email Messages (and Attachments) Using Delphi & Indy

Full Source Code For an Email Sender Application

Mail Sender Demo.

Below are instructions for creating an "email sender" that includes an option for sending email messages and attachments directly from a Delphi application. Before we begin, consider the alternative...

Suppose you have an application that operates on some database data, among other tasks. Users need to export data from your application and send the data through an email (like an error report). Without the approach outlined below, you have to export the data to an external file and then use an email client to send it.

Sending Email From Delphi

There are many ways you can send an email directly from Delphi, but the simplest way is to use the ShellExecute API. This will send the email using the default email client installed on the computer. While this approach is acceptable, you're unable to send attachments this way. 

Another technique uses Microsoft Outlook and OLE to send the email, this time with attachment support, but MS Outlook is then required to be used.

Yet another option is to use Delphi's built-in support for the Windows Simple Mail API. This works only if the user has a MAPI-compliant email program installed.

The technique we're discussing here uses Indy (Internet Direct) components - a great internet component suite comprised of popular internet protocols written in Delphi and based on blocking sockets.

The TIdSMTP (Indy) Method

Sending (or retrieving) email messages with Indy components (which ships with Delphi 6+) is as easy as dropping a component or two on a form, setting some properties, and "clicking a button."

To send an email with attachments from Delphi using Indy, we'll need two components. First, the TIdSMTOP is used to connect and communicate (send mail) with an SMTP server. Second, the TIdMessage handles the storing and encoding of the messages.

When the message is constructed (when TIdMessage is "filled" with data), the email is delivered to an SMTP server using the TIdSMTP.

Email Sender Source Code

I've created a simple mail sender project that I explain below. You can download the full source code here.

Note: That link is a direct download to the ZIP file for the project. You should be able to open it without any problems, but if you can't, use 7-Zip to open the archive so you can extract out the project files (which are stored in a folder called SendMail).

As you can see from the design-time screenshot, to send an email using the TIdSMTP component, you at least need to specify the SMTP mail server (host). The message itself needs the regular email parts filled out, like the From, To, Subject, etc.

Here's the code that handles sending one email with an attachment:

 procedure TMailerForm.btnSendMailClick(Sender: TObject) ;
  //setup SMTP
  SMTP.Host := ledHost.Text;
  SMTP.Port := 25;
  //setup mail message
  MailMessage.From.Address := ledFrom.Text;
  MailMessage.Recipients.EMailAddresses := ledTo.Text + ',' + ledCC.Text;
  MailMessage.Subject := ledSubject.Text;
  MailMessage.Body.Text := Body.Text;
  if FileExists(ledAttachment.Text) then TIdAttachment.Create(MailMessage.MessageParts, ledAttachment.Text) ;
  //send mail
      SMTP.Connect(1000) ;
      SMTP.Send(MailMessage) ;
    except on E:Exception do
      StatusMemo.Lines.Insert(0, 'ERROR: ' + E.Message) ;
    if SMTP.Connected then SMTP.Disconnect;
end; (* btnSendMail Click *) 

Note: Inside the source code, you'll find two extra procedures that are used to make the values of the Host, From, and To edit boxes persistent, using an INI file for storage.

mla apa chicago
Your Citation
Gajic, Zarko. "Send Email Messages (and Attachments) Using Delphi & Indy." ThoughtCo, Apr. 5, 2023, Gajic, Zarko. (2023, April 5). Send Email Messages (and Attachments) Using Delphi & Indy. Retrieved from Gajic, Zarko. "Send Email Messages (and Attachments) Using Delphi & Indy." ThoughtCo. (accessed May 31, 2023).