Site Map Contact Us Home
E-mail Newsletter
Subscribe to get informed about
Clever Components news.

Your Name:
Your Email:
 
SUBSCRIBE
 
Previous Newsletters
 




Products Articles Downloads Order Support
Customer Portal      

Clever Mailer for RAD Studio

Clever Mailer for RAD Studio

The Clever Mailer for RAD Studio is a console application that allows you to send email messages in the text or HTML format with the DKIM signature to the specified recipient list.

This article describes how to compose email message, put DKIM signature, connect to SMTP service and send the message to the list of recipients. The sending status is stored to the report file in text format.

Abstract

The algorithm of the mailer program is simple:

  • Read configuration;
  • Set up DKIM component;
  • Compose the mail message in the appropriate format;
  • Loop through the recipient list and send mail;
  • Write report.

Lets go ahead and consider each of these steps in details.

Read configuration information

The program uses a special configuration file in "ini" format. So we can use the TIniFile class for loading this information:

// [Delphi]
procedure TMailer.ReadConfiguration(const AConfigFile: string);
var
   ini: TIniFile;
begin
   ini := TIniFile.Create(AConfigFile);
   try
      FHost := ini.ReadString('SMTP', 'Host', '');
      FPort := ini.ReadInteger('SMTP', 'Port', cDefaultSmtpPort);
      FUserName := ini.ReadString('SMTP', 'User', '');
      FPassword := ini.ReadString('SMTP', 'Pass', '');

      FDomain := ini.ReadString('DKIM', 'Domain', '');
      FSelector := ini.ReadString('DKIM', 'Selector', '');
      FKeyFile := ini.ReadString('DKIM', 'KeyFile', '');

      FSubject := ini.ReadString('MAIL', 'Subj', '');
      FFrom := ini.ReadString('MAIL', 'From', '');
      FTextBodyFileName := ini.ReadString('MAIL', 'Text', '');
      FHtmlBodyFileName := ini.ReadString('MAIL', 'Html', '');

      FRecipientsFileName := ini.ReadString('RECIPIENTS', 'List', '');
   finally
      ini.Free();
   end;
end;

The message body can be represented in text, HTML or both formats. Each message body part is loaded from the file on the disk. The recipient list is loaded from the file as well.

// [Delphi]
procedure TMailer.LoadMessageData(ATextTemplate, AHtmlTemplate, ARecipients: TStrings);
begin
   if FileExists(TextBodyFileName) then
   begin
      ATextTemplate.LoadFromFile(TextBodyFileName, TEncoding.UTF8);
   end;

   if FileExists(HtmlBodyFileName) then
   begin
      AHtmlTemplate.LoadFromFile(HtmlBodyFileName, TEncoding.UTF8);
   end;

   if FileExists(RecipientsFileName) then
   begin
      ARecipients.LoadFromFile(RecipientsFileName, TEncoding.UTF8);
   end;
end;

You may need to substitute the name of the recipient to the message body before sending. The TclEmailAddressItem class from Clever Internet Suite allows you to extract the name part from the fully qualified email address information:

// [Delphi]
function TMailer.MergeRecipient(ABodyTemplate: TStrings; const ARecipient: string): string;
var
   addr: TclEmailAddressItem;
begin
   addr := TclEmailAddressItem.Create();
   try
      addr.FullAddress := ARecipient;
      Result := ABodyTemplate.Text;
      Result := StringReplace(ABodyTemplate.Text, ':RECIPIENT', addr.Name, [rfReplaceAll]);
   finally
      addr.Free();
   end;
end;

Set up the DKIM component

The DKIM component requires the following information for signing the message: domain, selector and a private key. This information is loaded from the configuration file and from the specified key file in the "pem" format. The detailed description for the domain and selector parameters can be found in the DKIM standard RFC 6376

// [Delphi]
function TMailer.CreateDkim: TclDkim;
begin
   Result := TclDkim.Create(nil);
   try
      Result.Canonicalization := 'relaxed/relaxed';
      Result.Domain := Domain;
      Result.Selector := Selector;
      Result.ImportPrivateKey(KeyFile);
   except
      Result.Free();
      raise;
   end;
end;

The key information represents the private part of the public / private key pair that is specific to the sender's domain. If you don't have a key, you can generate it with the DKIM component from the Clever Internet Suite library. A sample of this functionality can be found in the DkimSign demo. You can download it at Clever Internet Suite Download

Loop through the recipient list and send mail

After the whole data is ready and the components are configured, you can loop through the recipient list and send mail to each of them. There is one important moment: you need to check whether the message has been aready sent before starting the process. Otherwise, the message may be sent twice or more times.

// [Delphi]
...
CheckSentStatus();
LoadMessageData(textTemplate, htmlTemplate, recipients);
LoginSmtp(smtp);
dkim := CreateDkim();

for i := 0 to recipients.Count - 1 do
begin
   SendMessageTo(smtp, dkim, textTemplate, htmlTemplate, recipients[i]);
end;
...

// [Delphi]
procedure TMailer.SendMessageTo(ASmtp: TclSmtp; ADkim: TclDkim; ATextTemplate, AHtmlTemplate: TStrings; const ARecipient: string);
var
   text, html: string;
   msg: TclMailMessage;
begin
   text := MergeRecipient(ATextTemplate, ARecipient);
   html := MergeRecipient(AHtmlTemplate, ARecipient);
   msg := TclMailMessage.Create(nil);
   try
      msg.Dkim := ADkim;

      msg.CharSet := 'UTF-8';
      msg.BuildMessage(text, html);
      msg.Subject := Subject;
      msg.From.FullAddress := From;
      msg.ToList.Add(ARecipient);

      ASmtp.Send(msg);
   finally
      msg.Free();
   end;
end;

Write report

The sending status can be stored in a report file for further analyzing. Note, the sending status may differ from the delivery status. The sending status indicates that the message was sent successfully at the client side. The most of email services may initially accept the email message, but not deliver to the recipient's mailbox. There may be different reasons why the message is not delivered: email filtering, mailbox unavailable, mailbox is full, etc. In such a case, you receive the non-delivery report. This report is sent by the email service to the sender's mailbox. The analysis of these non-delivery reports is out of scope of this article. We will consider this task in the next article. This functionality may be implemented with the BounceChecker component of the Clever Internet Suite library.

Supported compiler versions

Clever Mailer for RAD Studio can be used in RAD Studio XE3 and higher, including RAD Studio 10.1 Berlin. If you modify the sources and remove all references to the RAD Studio namespaces in the 'uses' sections, you can use the library in older versions of RAD Studio, CodeGear or Borland Delphi.

Download source code

The program is distributed under the terms of the GNU Lesser General Public License version 3GNU General Public License

The current version of Clever Mailer for RAD Studio needs for the non-free library Clever Internet Suite. This is a drawback, and we suggest the task of changing the program so that it does the same job without the non-free library. Anyone who thinks of doing substantial further work on the program, first may free it from dependence on the non-free library. The class structure allows you to easily replace the Clever Internet Suite MailMessage, Smtp and Dkim components with any other third-party free and non-free software.

  Name   Download
 Clever Mailer for RAD Studio Download on GitHub
 Internet Components - Clever Internet Suite Download

Please feel free to Contact us if you need any assistance.

 

Sergey Shirokov
Clever Components team
www.clevercomponents.com

    Copyright © 2000-2024