EASendMail SMTP Component > Developer Center > Send Email in Delphi - Tutorial

Send Email in Delphi - Tutorial

Introduction

EASendMail is a SMTP component which supports all operations of SMTP/ESMTP protocols (RFC 821, RFC 822, RFC 2554). This tutorial covers everything of sending email with EASendMail in Delphi.

Installation

Before you can use the following sample codes, you should download the EASendMail Installer and install it on your machine at first.

A simple Delphi project

To better demonstrate how to use EASendMail sending email, let's create a Delphi Standard EXE project at first, then add a TButton on the Form, double-click this button. It is like this:

Delphi form project

Add Reference of EASendMail ActiveX Object to Delphi Project

To use EASendMail SMTP ActiveX Object in your project, the first step is "Add reference of EASendMail to your project". Please choose menu->"Project" -> "Import Type Library"- > and choose the "EASendMailObj ActiveX Object", click "Create Unit", the reference of EASendMail ActiveX Object will be added to your project, and you can start to use EASendMail to send email in your project.

add reference in Delphi

[Delphi Example - Send email]

Now add the following codes to the project and change From, To, Server, User and Password to corresponding value.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, EASendMailObjLib_TLB; // add EASendMail unit

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  oSmtp : TMail;
begin
  oSmtp := TMail.Create(Application);
  oSmtp.LicenseCode := 'TryIt';

  // Set your sender email address
  oSmtp.FromAddr := 'test@emailarchitect.net';

  // Add recipient email address
  oSmtp.AddRecipientEx( 'support@emailarchitect.net', 0);

  // Set email subject
  oSmtp.Subject := 'simple email from Delphi project';

  // Set email body
  oSmtp.BodyText := 'this is a test email sent from Delphi project, do not reply';

  // Your SMTP server address
  oSmtp.ServerAddr := 'smtp.emailarchitect.net';

  // User and password for ESMTP authentication, if your server doesn't require
  // user authentication, please remove the following codes
  oSmtp.UserName := 'test@emailarchitect.net';
  oSmtp.Password := 'testpassword';

  // If your SMTP server requires SSL connection, please add this line
  // oSmtp.SSL_init();

  ShowMessage( 'start to send email ...' );

  if oSmtp.SendMail() = 0 then
    ShowMessage( 'email was sent successfully!' )
  else
    ShowMessage( 'failed to send email with the following error: '
    + oSmtp.GetLastErrDescription());

end;

end.

If you set everything right, you can get "email was sent successfully". If you get "failed to send email with the following error:", then please have a look at the following section.

Where can I get my SMTP email server address, user and password?

Because each email account provider has different server address, so you should query your SMTP server address from your email account provider. To prevent spreading email from the server, most SMTP servers also require user authentication. User name is your email address or your email address without domain part, it depends on your email provider setting.

When you execute above example code, if you get error about "Networking connection" or "No such host", it is likely that your SMTP server address is not correct. If you get an error like "5xx Relay denied", it is likely that you did not set user authentication. Another common error is "5xx Must issue a STARTTLS command first" or "No supported authentication marshal found!", that is because your SMTP server requires user authentication under SSL connection. You can set the SSL connection to solve this problem.

Finally, if you have already set your account in your email client such as Outlook or Window Mail, you can query your SMTP server address, user in your email client. For example, you can choose menu -> "Tools" - > - "Accounts" - > "Your email account" - > "Properties" - > "Servers" in Outlook express or Windows Mail to get your SMTP server, user. Using EASendMail to send email does not require you have email client installed on your machine or MAPI, however you can query your exist email accounts in your email client.

Delphi console email sample

Email Address Syntax and Multiple Recipients

Mail Address Syntax in EASendMail SMTP Component:

For single email address (From, ReplyTo, ReturnPath), the syntax can be: ["][display name]["]<email address>.
For example: "Tester, T" <test@adminsystem.com>, Tester <test@adminsystem.com>, <test@adminsystem.com> or test@adminsystem.com.

For mulitple email address (To, CC, Bcc), the syntax can be: [single email],[single email]... (,;\r\n) can be used to separate multiple email addresses.
For example: "Tester, T" <test1@adminsystem.com>, Tester2 <test2@adminsystem.com>, <test3@adminsystem.com>, test4@adminsystem.com

[Delphi Example - Email syntax]

To better understand the email address syntax, please refer to the following codes.

oSmtp.FromAddr := 'Tester<test@adminsystem.com>';
oSmtp.FromAddr := 'test@adminsystem.com';
oSmtp.FromAddr := '<test@adminsystem.com>';

// Using AddRecipientEx to add To, Cc and Bcc in Delphi/Visual Basic 6.0
// Multiple addresses are separated with (,)
// The syntax is like this: "test@adminsystem.com, test1@adminsystem.com"
oSmtp.AddRecipientEx( 'test1@adminsystem.com, test2@adminsystem.com', 0 );
oSmtp.AddRecipientEx( 'Test1<test@adminsystem.com>, Test2<test2@adminsystem.com>', 0 );

// You can also add carbon copy (CC) or blind carbon copy (BCC) in the email.
oSmtp.AddRecipientEx( 'CC recipient<cc@adminsystem.com>', 1 );
oSmtp.AddRecipientEx( 'Bcc recipient<bcc@adminsystem.com>', 2 );

Reply-To, Return-Path and Mail Priority

If you want to set another email address to get the replied email rather than your From address, you can use ReplyTo property.

If you want to set another email address to get the delivery report rather than your From address, you can use ReturnPath property.

If you want to set Higher or Lower priority to your email, you can use Priority prority

[Delphi Example - ReplyTo, ReturnPath and Priority]

oSmtp.FromAddr := 'Tester <test@emailarchitect.net>';

// Set the Reply-To address
oSmtp.ReplyTo := 'replyto@@emailarchitect.net';

// Set the email address to receive delivery report
oSmtp.ReturnPath := 'report@emailarchitect.net';

// Set high priority
oSmtp.Priority = 1;
Next Section

In this section, I introduced the basic things of sending email in Delphi with EASendMail. At next section I will introduce how to send email over SSL connection.

Next: Send email over SSL in Delphi ->

Comments

If you have any comments or questions about above example codes, please click here to add your comments.

Download

Send Email - C# - VB6 - Visual Basic - VC++ - Managed C++ - Delphi

Retrieve Email and Parse Email - C# - VB6 - Visual Basic - VC++ - Managed C++ - Delphi

Email Solution - Email Server - DomainKeys/DKIM - Disclaimer

2003 - 2011 © Copyright AdminSystem Software Limited. All rights reserved.   About us     Follow emailarchitect on Twitter