从Delphi 2007应用程序发送电子邮件
我有一个传统的delphi 2007应用程序,它通过TurboPower Internet Professional 1.15(tpipro)发送电子邮件警报。我最近重新访问了该应用程序,发现由于大多数电子邮件服务器的TLS/SSL要求,电子邮件发送不再有效。现在我的问题是从这里走到哪里 我有Delphi XE2,但我真的不想花时间更新我的应用程序以在这个ide上工作。它有许多库依赖关系等等从Delphi 2007应用程序发送电子邮件,delphi,email,delphi-2007,Delphi,Email,Delphi 2007,我有一个传统的delphi 2007应用程序,它通过TurboPower Internet Professional 1.15(tpipro)发送电子邮件警报。我最近重新访问了该应用程序,发现由于大多数电子邮件服务器的TLS/SSL要求,电子邮件发送不再有效。现在我的问题是从这里走到哪里 我有Delphi XE2,但我真的不想花时间更新我的应用程序以在这个ide上工作。它有许多库依赖关系等等 是否有最新的第三方电子邮件客户端将在Delphi 2007上运行?或者是可以使用的.dll?您可以使用d
是否有最新的第三方电子邮件客户端将在Delphi 2007上运行?或者是可以使用的.dll?您可以使用delphi中包含的
Indy
库,这些组件支持TLS和SSL(查看TIdSmtp组件),您可以找到Indy的最新版本。只是为了给您提供更多选项
你也可以试试IPWorks,它不是免费的,你可以找到它,或者你可能想看看ICS(互联网组件套件),它是免费的,你可以找到它
Indy是一个明显的选择,因为它是与Delphi XE2一起安装的(你可以用VCL类替换我自己的类来让它工作):
单元SmtpClientUnt;
接口
使用
类、IdSslOpenSsl、IdSmtp、CsiBaseObjectsUnt、devExceptionCount;
类型
ESmtpClient=class(EdeveException);
TSmtpClient=类密封(TCsiBaseObject)
私有的
FHostName:字符串;
FIdSmtpClient:TIdSmtp;
FIoHandler:TIdSslIoHandlerSocketOpenSsl;
FUseTls:布尔型;
受保护的
程序检查等参线(常数:字符串);
函数GetHostName:string;事实上的
函数GetIsOpen:Boolean;事实上的
函数GetObjectName:string;推翻
公众的
常量LC_SMTP_客户端='SMTP客户端';
构造函数创建(constphostname:string;pUseTls:Boolean=False);
毁灭者毁灭;推翻
程序关闭;
过程打开(const-pUserName:string='';const-pPassword:string='';
程序重新连接;
过程SendMessage(pToAddresses:TStrings;const pFromAddress:string;
常量PSObject:string;常量pBody:string;
pAttachmentFiles:TStrings=nil);
属性主机名:字符串读取GetHostName;
属性等参线:布尔读取等参线;
结束;
实施
使用
SysUtils、IdAttachmentFile、IdEmailAddress、IdExplicitTlsClientServerBase、IdMessage、,
CSIexceptionCount、CSIglobalCount、CSIsingletonManagerRunt、CsiStringsUnt;
{TSmtpClient}
过程TSmtpClient.CheckIsOpen(const-pEventAction:string);
开始
如果不是,那么
引发ESmtpClient.Create('Cannot'+pEventAction+
“SMTP客户端未打开时”,slError,1,
对象名称);
结束;
程序TSmtpClient.Close;
开始
如果是,那么开始
CsiGlobals.AddLogMsg('关闭SMTP客户端',LC_SMTP_客户端,llVerbose,ObjectName);
FIdSmtpClient.断开连接;
结束;
结束;
构造函数TSmtpClient.Create(constphostname:string;pUseTls:Boolean);
开始
FHostName:=磷名称;
FUseTls:=pUseTls;
继承创造;
如果FHostName='',则
引发ESmtpClient.Create('无法创建SMTP客户端-空主机名',错误,2,
对象名称);
FIdSmtpClient:=tidstp.Create(无);
FIdSmtpClient.Host:=pHostName;
如果是FUseTls,则开始
FIoHandler:=TIdSslIoHandlerSocketOpenSsl.Create(nil);
FIdSmtpClient.IoHandler:=fiohhandler;
FIdSmtpClient.UseTls:=utUseRequireTls;
结束;
结束;
析构函数TSmtpClient.Destroy;
开始
接近;
如果FUseTls和Assigned(FIdSmtpClient),则开始
FIdSmtpClient.IoHandler:=nil;
FreeAndNil(菲奥汉德勒);
结束;
FreeAndNil(FIdSmtpClient);
继承;
结束;
函数TSmtpClient.GetHostName:string;
开始
如果已分配(FIdSmtpClient),则
结果:=FIdSmtpClient.Host
其他的
结果:=FHostName;
结束;
函数TSmtpClient.GetIsOpen:Boolean;
开始
结果:=已分配(FIdSmtpClient)和FIdSmtpClient.Connected;
结束;
函数TSmtpClient.GetObjectName:string;
变量
lHostName:string;
开始
结果:=继承的GetObjectName;
lHostName:=主机名;
如果名称为“”,则
结果:=结果+“”+lHostName;
结束;
过程TSmtpClient.Open(const pUserName:string;const pPassword:string);
开始
如果不是,则开始
使用FIdSmtpClient开始
用户名:=pUserName;
密码:=pPassword;
连接
结束;
CsiGlobals.AddLogMsg('SMTP客户端已打开',LC_SMTP_客户端,llVerbose,ObjectName);
结束;
结束;
程序TSmtpClient.重新连接;
开始
接近;
打开
结束;
过程TSmtpClient.SendMessage(pToAddresses:TStrings;const-pFromAddress:string;
常量PSObject:string;常量pBody:string;
pAttachmentFiles:TStrings);
变量
信息:TID信息;
衣服:细绳;
lName:字符串;
lIndex:整数;
lAddressItem:TIdEMailAddressItem;
lAttachmentFile:TIdAttachmentFile;
lFileName:字符串;
开始
CheckIsOpen(“发送消息”);
l消息:=TIdMessage.Create(无);
尝试
用我的话开始吧
CsiSplitFirstStr(pFromAddress,,,lAddress,lName);
发件人。地址:=lAddress;
From.Name:=lName;
主题:=PSObject;
Body.Text:=pBody;
结束;
对于lIndex:=0到0个地址。计数-1开始
lAddressItem:=lMessage.Recipients.Add;
CsiSplitFirstStr(pToAddresses.Strings[lIndex],',',lAddress,lName);
lAddress项目。地址:=lAddress;
lAddressItem.Name:=lName;
结束;
如果已分配(pAttachmentFiles),则
对于lIndex:=0到pAttachmentFiles。计数-1开始
lAttachmentFile:=TIdAttachmentFile.Create(lMessage.MessageParts);
lFileName:=pAttachmentFiles.Strings[lIndex];
lAttachmentFile.StoredPathName:=lFileName;
lAttachmentFile.FileName:=lFileName;
结束;
FIdSmtpClient.Send(lMessage);
最后
信息F
unit SmtpClientUnt;
interface
uses
Classes, IdSslOpenSsl, IdSmtp, CsiBaseObjectsUnt, DevExceptionsUnt;
type
ESmtpClient = class(EDevException);
TSmtpClient = class sealed(TCsiBaseObject)
private
FHostName: string;
FIdSmtpClient: TIdSmtp;
FIoHandler: TIdSslIoHandlerSocketOpenSsl;
FUseTls: Boolean;
protected
procedure CheckIsOpen(const pEventAction: string);
function GetHostName: string; virtual;
function GetIsOpen: Boolean; virtual;
function GetObjectName: string; override;
public
const LC_SMTP_CLIENT = 'SMTP Client';
constructor Create(const pHostName: string; pUseTls: Boolean = False);
destructor Destroy; override;
procedure Close;
procedure Open(const pUserName: string = ''; const pPassword: string = '');
procedure Reconnect;
procedure SendMessage(pToAddresses: TStrings; const pFromAddress: string;
const pSubject: string; const pBody: string;
pAttachmentFiles: TStrings = nil);
property HostName: string read GetHostName;
property IsOpen: Boolean read GetIsOpen;
end;
implementation
uses
SysUtils, IdAttachmentFile, IdEmailAddress, IdExplicitTlsClientServerBase, IdMessage,
CsiExceptionsUnt, CsiGlobalsUnt, CsiSingletonManagerUnt, CsiStringsUnt;
{ TSmtpClient }
procedure TSmtpClient.CheckIsOpen(const pEventAction: string);
begin
if not IsOpen then
raise ESmtpClient.Create('Cannot ' + pEventAction +
' while the SMTP client is not open', slError, 1,
ObjectName);
end;
procedure TSmtpClient.Close;
begin
if IsOpen then begin
CsiGlobals.AddLogMsg('Closing SMTP client', LC_SMTP_CLIENT, llVerbose, ObjectName);
FIdSmtpClient.Disconnect;
end;
end;
constructor TSmtpClient.Create(const pHostName: string; pUseTls: Boolean);
begin
FHostName := pHostName;
FUseTls := pUseTls;
inherited Create;
if FHostName = '' then
raise ESmtpClient.Create('Cannot create SMTP client - empty host name', slError, 2,
ObjectName);
FIdSmtpClient := TIdSmtp.Create(nil);
FIdSmtpClient.Host := pHostName;
if FUseTls then begin
FIoHandler := TIdSslIoHandlerSocketOpenSsl.Create(nil);
FIdSmtpClient.IoHandler := FIoHandler;
FIdSmtpClient.UseTls := utUseRequireTls;
end;
end;
destructor TSmtpClient.Destroy;
begin
Close;
if FUseTls and Assigned(FIdSmtpClient) then begin
FIdSmtpClient.IoHandler := nil;
FreeAndNil(FIoHandler);
end;
FreeAndNil(FIdSmtpClient);
inherited;
end;
function TSmtpClient.GetHostName: string;
begin
if Assigned(FIdSmtpClient) then
Result := FIdSmtpClient.Host
else
Result := FHostName;
end;
function TSmtpClient.GetIsOpen: Boolean;
begin
Result := Assigned(FIdSmtpClient) and FIdSmtpClient.Connected;
end;
function TSmtpClient.GetObjectName: string;
var
lHostName: string;
begin
Result := inherited GetObjectName;
lHostName := HostName;
if lHostName <> '' then
Result := Result + ' ' + lHostName;
end;
procedure TSmtpClient.Open(const pUserName: string; const pPassword: string);
begin
if not IsOpen then begin
with FIdSmtpClient do begin
Username := pUserName;
Password := pPassword;
Connect;
end;
CsiGlobals.AddLogMsg('SMTP client opened', LC_SMTP_CLIENT, llVerbose, ObjectName);
end;
end;
procedure TSmtpClient.Reconnect;
begin
Close;
Open;
end;
procedure TSmtpClient.SendMessage(pToAddresses: TStrings; const pFromAddress: string;
const pSubject: string; const pBody: string;
pAttachmentFiles: TStrings);
var
lMessage: TIdMessage;
lAddress: string;
lName: string;
lIndex: Integer;
lAddressItem: TIdEMailAddressItem;
lAttachmentFile: TIdAttachmentFile;
lFileName: string;
begin
CheckIsOpen('send message');
lMessage := TIdMessage.Create(nil);
try
with lMessage do begin
CsiSplitFirstStr(pFromAddress, ',', lAddress, lName);
From.Address := lAddress;
From.Name := lName;
Subject := pSubject;
Body.Text := pBody;
end;
for lIndex := 0 to pToAddresses.Count - 1 do begin
lAddressItem := lMessage.Recipients.Add;
CsiSplitFirstStr(pToAddresses.Strings[lIndex], ',', lAddress, lName);
lAddressItem.Address := lAddress;
lAddressItem.Name := lName;
end;
if Assigned(pAttachmentFiles) then
for lIndex := 0 to pAttachmentFiles.Count - 1 do begin
lAttachmentFile := TIdAttachmentFile.Create(lMessage.MessageParts);
lFileName := pAttachmentFiles.Strings[lIndex];
lAttachmentFile.StoredPathName := lFileName;
lAttachmentFile.FileName := lFileName;
end;
FIdSmtpClient.Send(lMessage);
finally
lMessage.Free;
end;
end;
procedure InitialiseUnit;
begin
CsiAllCapWords.AddString('SMTP');
end;
initialization
CsiSingletonManager.RegisterHook(InitialiseUnit, nil);
end.