Sunday, June 29, 2008

Sending email messages in Delphi

Learn how to send email messages with attachments using Indy and Delphi. Full source code to a simple "SMTP Mail Sender" application included.

Let's get straight to the problem ... 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 attached to an email address (to you, an error report, for example). For the moment, you are exporting data to an external file, and than Outlook (Express) is being used to send the email.

Let's see how create a more powerful application by including an option to send email messages, with attachments, directly from your Delphi application.

Go email, go
There are many ways you can send an email directly from Delphi, the most simple one is to use the ShellExecute API call to send an email using the default e-mail client software installed on a user's machine, this approach is ok, but you are unable to send attachments in this way. Another technique uses MS Outlook and OLE to send email with attachment, this one requires that Outlook is installed. Another approach is to use Delphi's built-in support for the Windows Simple Mail API (only works if the end-user has MAPI-compliant email software).

The approach we are discussing here uses Indy components - a great internet component suite comprised of popular internet protocols written in Delphi and based on blocking sockets.

The TIdSMTP (Indy) way
Sending (or retrieving) email messages with Indy components (ship 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 attachment(s) 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 storing and encoding of message(s).
When the message is constructed (TIdMessage "filled" with data), the email message is delivered to an SMTP server using the TIdSMTP.

Mail sender
I've created a simple mail sender project; you can download the full source code.

As you can see from the design-time screen shot, to send an email using the TIdSMTP component, you at least need to specify the SMTP mail server (host). The message itself needs the "From", "To", "Subject", ..., and all other parts you've accustomed with.

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

procedure TMailerForm.btnSendMailClick(Sender: TObject);
begin
StatusMemo.Clear;

//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
try
try
SMTP.Connect(1000);
SMTP.Send(MailMessage);
except on E:Exception do
StatusMemo.Lines.Insert(0, 'ERROR: ' + E.Message);
end;
finally
if SMTP.Connected then
SMTP.Disconnect;
end;

end; (* btnSendMail Click *)

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

/resource from http://delphi.about.com/

0 comments: