In previous section,I introduced how to send mass email with EASendMail Service database queue. In this section, I will introduce how to send email using Exchange Web Service (EWS) in Delphi.
Sections:
Exchange Web Services (EWS), an alternative to the MAPI protocol, is a documented SOAP based protocol introduced with Exchange Server 2007. We can use HTTP or HTTPS protocol to send email with Exchange Web Services (EWS) instead of SMTP protocol.
With EASendMail SMTP Component, you do not have to build your EWS SOAP XML request and parse the response. It wraps the SOAP XML and HTTP request automatically. You just need to change the Protocol property, and then EASendMail uses Web Service protocol to send email. Your server SHOULD be Exchange 2007 or later version; otherwise you cannot use Exchange Web Service protocol.
SMTP protocol
Standard SMTP protocol based on TCP/IP, all email servers support this protocol, Exchange Server also supports SMTP protocol. Using SMTP protocol is always recommended.
Exchange WebDAV
Exchange WebDAV is a set of methods based on the HTTP protocol to manage users, messages in Microsoft Exchange Server. We can use HTTP or HTTP/HTTPS protocol to send email with Exchange WebDAV instead of SMTP protocol. But since Exchange 2007, WebDAV service is disabled by default, so I only suggest that you use WebDAV protocol in Exchange 2000/2003.
Exchange Web Service (EWS)
Exchange Web Services (EWS), an alternative to the MAPI protocol, is a documented SOAP based protocol introduced with Exchange Server 2007. We can use HTTP or HTTPS protocol to send email with Exchange Web Services (EWS) instead of SMTP protocol. I only suggest that you use EWS protocol in Exchange 2007/2010/2013/2016 or later version. Office365 also supports EWS very well.
Note
Remarks: All of samples in this section are based on first section: Send email in a simple Delphi project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.
The following example codes demonstrate how to send email using Exchange Web Service (EWS) in Delphi.
Note
To get the full sample projects, please refer to Samples section.
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;
const
ConnectNormal = 0;
ConnectSSLAuto = 1;
ConnectSTARTTLS = 2;
ConnectDirectSSL = 3;
ConnectTryTLS = 4;
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 Exchange server address
oSmtp.ServerAddr := 'exch.emailarchitect.net';
// Set Exchange Web Service Protocol - EWS - Exchange 2007/2010/2013/2016
oSmtp.Protocol := 1;
// User and password for Exchange user authentication
oSmtp.UserName := 'test';
oSmtp.Password := 'testpassword';
// By default, Exchange Web Service requires SSL connection
oSmtp.ConnectType := ConnectSSLAuto;
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 send email to Exchange Server 2007 using EWS. You may get an exception: “Client does not have permissions to send as this sender”. You should add this line in your code:
// Set sender email address, please change it to yours
oSmtp.FromAddr := 'test@emailarchitect.net';
oSmtp.Anonymous := 1;
That is because Exchange Server 2007 doesn’t allow From header in the email message, the From header will be added by Exchange Server automatically.
If you send email using SMTP protocol and it threw an exception: “Client does not have permissions to send as this sender” with Exchange 2007/2010/2013, please see the following topic:
Next Section
At next section I will introduce how to send email using Exchange WebDAV in Delphi.
Appendix
Comments
If you have any comments or questions about above example codes, please click here to add your comments.