Windows 使用Indy组件发送电子邮件
我尝试发送电子邮件,但我有一个问题,但是,我在web上发现以下代码:Windows 使用Indy组件发送电子邮件,windows,delphi,email,compiler-construction,indy,Windows,Delphi,Email,Compiler Construction,Indy,我尝试发送电子邮件,但我有一个问题,但是,我在web上发现以下代码: Uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdMessage, IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP, IdBaseComponent, IdComponent, IdIOHandler, IdExpli
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;
但是,我注意到了很多错误,我遗漏了印地的一个组成部分。
编译器错误:
[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'
提前感谢您的帮助对于谷歌smtp,您需要使用TLS或SSL! 您的过程示例是为INDY9编写的,如果使用INDY10,则无法编译。
您需要进行调整。您的问题中的代码是为Indy 9编写的,而您的编译器错误似乎表明您正在使用Indy 10。要解决编译器错误,请执行以下操作:
[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'
-Undeclared identifier:Self
是指向类实例本身的指针,因为您没有将Self
作为类方法编写,而只是作为一个独立过程编写,所以您没有任何SendSimpleMail
,因为您没有任何类。例如,您可以为表单类编写类方法,例如Self
,其中TForm1.SendSimpleMail
在该方法中具有Self
实例的含义,即表单本身TForm1
- 其余的错误是因为您访问的是
类,而不是对象实例。常用的做法是声明一个局部变量,创建一个对象实例将其分配给该变量,使用该对象(变量)并释放该对象实例tidstp
一件事是在uses子句中添加
IdEMailAddress
,让编译器知道TIdEmailAddressItem
,但另一件事是这个示例基本上是错误的,它是针对Indy 9的(因为atLogin
身份验证类型),您使用的是哪个版本的Indy?您可以检查是否按住CTRL
键并从uses子句中单击例如IdSMTP
,然后检查IdSMTP.pas
的存储位置。如果它在Indy9
或Indy10
文件夹中。好的,谢谢,现在错误减少了,但现在有3个错误(我编辑了第一篇文章),我有Indy9和Indy10,但是当我单击idSMTP时,我得到一个错误:找不到文件idSMTP.pasNo,代码基本上是错误的。您需要创建一个tidmtp
的实例,而不是类本身。谢谢,但现在我有一个最后的错误:[DCC error]Unit1.pas(42):E2003未声明的标识符:“satDefault”我不确定,因为在中有atDefault
,但在Delphi 2009附带的版本中是satDefault
。因此,请尝试改用atDefault
。是的satDefault
最初在Indy 10早期版本中命名为atDefault
,后来被重命名。我只想添加一个注释。您必须添加IdEMailAddress才能使用TIdEmailAddressItem