SendEmail与Indy组件 [英] SendEmail with Indy components
问题描述
我尝试发送电子邮件,但我有一个问题,但是,我在网上找到这个代码:
使用
Windows,消息,SysUtils,变量,类,图形,控件,窗体,
对话框,StdCtrls,IdMessage,IdTCPConnection,IdTCPClient,
IdMessageClient,IdSMTP,IdBaseComponent,IdComponent,IdIOHandler,
IdExplicitTLSClientServerBase,IdSMTPBase
过程SendSimpleMail;
var
Msg:TIdMessage;
DestAddr:TIdEmailAddressItem;
begin
Msg:= TIdMessage.Create(Self); // error here
Msg.From.Text:='name';
Msg.From.Address:='username@gmail.com';
Msg.Subject:='Test';
DestAddr:= Msg.Recipients.Add;
DestAddr.Text:='name';
DestAddr.Address:='username@yahoo.com';
Msg.Body.Add('simple test mail。');
tIdSMTP.Host:='smtp.gmail.com';
tIdSMTP.Port:= 25;
tIdSMTP.AuthenticationType:= atLogin; // error here(2 error)
tIdSMTP.Username:='username@gmail.com';
tIdSMTP.Password:='password';
tIdSMTP.Connect;
tIdSMTP.Authenticate;
tIdSMTP.Send(Msg);
tIdSMTP.Disconnect;
end;
但是,我注意到许多错误,我错过了Indy的一个组件。
编译器错误:
[DCC错误] Unit1.pas(36):E2003未声明标识符:'Self'
[DCC错误] Unit1.pas(46):E2233属性'Host'无法访问
[DCC错误] Unit1.pas ] Unit1.pas(48):E2003未声明的标识符:'AuthenticationType'
[DCC错误] Unit1.pas(48):E2003未声明的标识符:'atLogin'
[DCC Error] Unit1.pas ):E2233属性'用户名'不可访问
[DCC错误] Unit1.pas(50):E2233属性'密码'不可访问
[DCC错误] Unit1.pas(51):E2076这种形式方法调用只允许类方法
[DCC Error] Unit1.pas(52):E2076这种形式的方法调用只允许类方法
[DCC Error] Unit1.pas(53):E2076形式的方法调用只允许类方法
[DCC Error] Unit1.pas(54):E2076这种形式的方法调用只允许类方法
[DCC Error] Project1.dpr(5): F2063无法编译已使用的单位'Unit1.pas'
感谢您的帮助
您的问题的代码是为Indy 9编写的,并且从您的编译器错误似乎使用Indy 10.对您的编译器错误: / p>
-
未声明的标识符:Self
-Self
是指向类实例本身的指针,由于您没有将SendSimpleMail
写为类方法,而只是作为独立过程,没有任何Self
,因为你没有任何类。你可以为你的表单类写的类方法,例如。TForm1.SendSimpleMail
,其中Self
将具有TForm1
实例,表单本身。 -
其余的错误,因为您访问
TIdSMTP
类,而不是对象实例。通常使用的做法是声明一个局部变量,创建一个对象实例,将其分配给该变量,使用该对象(变量)并释放该对象实例。
我会尝试这样(用Delphi 2009附带的Indy 10测试):
uses
IdSMTP,IdMessage,IdEMailAddress;
过程SendSimpleMail;
var
IdSMTP:TIdSMTP;
IdMessage:TIdMessage;
IdEmailAddressItem:TIdEmailAddressItem;
begin
IdSMTP:= TIdSMTP.Create(nil);
try
IdSMTP.Host:='smtp.gmail.com';
IdSMTP.Port:= 25;
IdSMTP.AuthType:= satDefault;
IdSMTP.Username:='username@gmail.com';
IdSMTP.Password:='password';
IdSMTP.Connect;
if IdSMTP.Authenticate then
begin
IdMessage:= TIdMessage.Create(nil);
try
IdMessage.From.Name:='User Name';
IdMessage.From.Address:='username@gmail.com';
IdMessage.Subject:='E-mail subject';
IdMessage.Body.Add('E-mail body。');
IdEmailAddressItem:= IdMessage.Recipients.Add;
IdEmailAddressItem.Address:='recipient@email.com';
IdSMTP.Send(IdMessage);
finally
IdMessage.Free;
end;
end;
IdSMTP.Disconnect;
finally
IdSMTP.Free;
end;
end;
I try to send an email, but I have a problem, however, I found this code on the web:
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdMessage, IdTCPConnection, IdTCPClient,
IdMessageClient, IdSMTP, IdBaseComponent, IdComponent, IdIOHandler,
IdExplicitTLSClientServerBase, IdSMTPBase
procedure SendSimpleMail;
var
Msg: TIdMessage;
DestAddr: TIdEmailAddressItem;
begin
Msg := TIdMessage.Create(Self); //error here
Msg.From.Text := 'name';
Msg.From.Address := 'username@gmail.com';
Msg.Subject := 'Test';
DestAddr := Msg.Recipients.Add;
DestAddr.Text := 'name';
DestAddr.Address := 'username@yahoo.com';
Msg.Body.Add('simple test mail.');
tIdSMTP.Host := 'smtp.gmail.com';
tIdSMTP.Port := 25;
tIdSMTP.AuthenticationType := atLogin; //error here (2 error)
tIdSMTP.Username := 'username@gmail.com';
tIdSMTP.Password := 'password';
tIdSMTP.Connect;
tIdSMTP.Authenticate;
tIdSMTP.Send(Msg);
tIdSMTP.Disconnect;
end;
But however, I noted many mistakes and I am missing a component of Indy. Compiler errors:
[DCC Error] Unit1.pas(36): E2003 Undeclared identifier: 'Self'
[DCC Error] Unit1.pas(46): E2233 Property 'Host' inaccessible here
[DCC Error] Unit1.pas(47): E2233 Property 'Port' inaccessible here
[DCC Error] Unit1.pas(48): E2003 Undeclared identifier: 'AuthenticationType'
[DCC Error] Unit1.pas(48): E2003 Undeclared identifier: 'atLogin'
[DCC Error] Unit1.pas(49): E2233 Property 'Username' inaccessible here
[DCC Error] Unit1.pas(50): E2233 Property 'Password' inaccessible here
[DCC Error] Unit1.pas(51): E2076 This form of method call only allowed for class methods
[DCC Error] Unit1.pas(52): E2076 This form of method call only allowed for class methods
[DCC Error] Unit1.pas(53): E2076 This form of method call only allowed for class methods
[DCC Error] Unit1.pas(54): E2076 This form of method call only allowed for class methods
[DCC Error] Project1.dpr(5): F2063 Could not compile used unit 'Unit1.pas'
Thanks for the help in advance
The code from your question is written for Indy 9 and from your compiler error seems you're using Indy 10. To your compiler errors:
Undeclared identifier: Self
- theSelf
is the pointer to the class instance itself and since you didn't write theSendSimpleMail
as a class method but just as a standalone procedure, you don't have anySelf
just because you don't have any class. The class method you could write for instance for your form class like e.g.TForm1.SendSimpleMail
, where inside of that method theSelf
would have meaning of theTForm1
instance, the form itself.And the rest of the errors you got because you were accessing the
TIdSMTP
class, not the object instance. Commonly used practice is to declare a local variable, create an object instance assigning it to that variable, work with that object (variable) and free the object instance.
I would try something like this (tested with Indy 10 shipped with Delphi 2009):
uses
IdSMTP, IdMessage, IdEMailAddress;
procedure SendSimpleMail;
var
IdSMTP: TIdSMTP;
IdMessage: TIdMessage;
IdEmailAddressItem: TIdEmailAddressItem;
begin
IdSMTP := TIdSMTP.Create(nil);
try
IdSMTP.Host := 'smtp.gmail.com';
IdSMTP.Port := 25;
IdSMTP.AuthType := satDefault;
IdSMTP.Username := 'username@gmail.com';
IdSMTP.Password := 'password';
IdSMTP.Connect;
if IdSMTP.Authenticate then
begin
IdMessage := TIdMessage.Create(nil);
try
IdMessage.From.Name := 'User Name';
IdMessage.From.Address := 'username@gmail.com';
IdMessage.Subject := 'E-mail subject';
IdMessage.Body.Add('E-mail body.');
IdEmailAddressItem := IdMessage.Recipients.Add;
IdEmailAddressItem.Address := 'recipient@email.com';
IdSMTP.Send(IdMessage);
finally
IdMessage.Free;
end;
end;
IdSMTP.Disconnect;
finally
IdSMTP.Free;
end;
end;
这篇关于SendEmail与Indy组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!