【问题标题】:SendEmail with Indy components带有 Indy 组件的 SendEmail
【发布时间】:2012-06-24 18:13:09
【问题描述】:

我尝试发送电子邮件,但我遇到了问题,然而,我在网上找到了这段代码:

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;

但是,我注意到了很多错误,并且我错过了 Indy 的一个组件。 编译器错误:

[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'

提前感谢您的帮助

【问题讨论】:

  • 一件事是将IdEMailAddress添加到您的uses子句中以使编译器知道TIdEmailAddressItem,但另一件事是该示例基本上是错误的,它适用于Indy 9(因为atLogin身份验证类型),您使用的是什么版本的 Indy?您可以检查是否按住 CTRL 键并单击例如IdSMTP 来自您的 uses 子句,然后检查 IdSMTP.pas 的存储位置。如果它在Indy9Indy10 文件夹中。
  • 好的,谢谢,现在错误减少了,但现在是 3 个错误(我编辑了第一篇文章),我有 Indy9 和 Indy10,但是当我点击 idSMTP 时出现错误:无法定位文件 idSMTP.pas

标签: windows delphi email compiler-construction indy


【解决方案1】:

对于 google smtp,您需要使用 TLS 或 SSL! http://support.google.com/mail/bin/answer.py?hl=en&answer=13287

您的程序示例是为 INDY9 编写的,如果您使用 INDY10,则无法编译。 你需要做出调整。

【讨论】:

  • 不行,代码基本是错的。您需要创建tIdSMTP 的实例,而不是与类本身一起使用。
【解决方案2】:

您问题中的代码是为 Indy 9 编写的,并且您的编译器错误似乎您正在使用 Indy 10。对于您的编译器错误:

  • Undeclared identifier: Self - Self 是指向类实例本身的指针,由于您没有将 SendSimpleMail 作为类方法编写,而是作为独立过程编写,因此您没有任何Self 只是因为你没有任何课程。例如,您可以为表单类编写的类方法,例如TForm1.SendSimpleMail,在该方法内部,Self 将具有 TForm1 实例的含义,即表单本身。

  • 还有其他错误,因为您访问的是TIdSMTP 类,而不是对象实例。常用的做法是声明一个局部变量,创建一个对象实例并将其分配给该变量,使用该对象(变量)并释放该对象实例。

我会尝试这样的事情(使用 Delphi 2009 附带的 Indy 10 进行测试):

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;

【讨论】:

  • 谢谢,但现在我遇到了最后一个错误:[DCC Error] Unit1.pas(42): E2003 Undeclared identifier: 'satDefault'
  • 我不确定,因为在reference 中有atDefault,但在Delphi 2009 附带的版本中它是satDefault。所以尝试改用atDefault
  • 是的 satDefault 在早期的 Indy 10 版本中最初被命名为 atDefault,后来被重命名。
  • 我只想添加一个注释。您必须添加 IdEMailAddress 才能使用 TIdEmailAddressItem
猜你喜欢
  • 1970-01-01
  • 2017-07-29
  • 2013-03-14
  • 1970-01-01
  • 2011-11-18
  • 2012-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多