加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – 带有Indy组件的SendEmail

发布时间:2020-12-13 20:32:30 所属栏目:Windows 来源:网络整理
导读:我尝试发送电子邮件,但是我有一个问题,但是,我在网上找到了这个代码: UsesWindows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,IdMessage,IdTCPConnection,IdTCPClient,IdMessageClient,IdSMTP,IdBaseComponent,IdCompon
我尝试发送电子邮件,但是我有一个问题,但是,我在网上找到了这个代码:
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'

我在这里先向您的帮助表示感谢

您的问题中的代码是为Indy 9编写的,并且从您的编译器错误看起来您正在使用Indy 10.对于您的编译器错误:

>未声明的标识符:Self – Self是指向类实例本身的指针,因为您没有将SendSimpleMail编写为类方法,而只是作为一个独立的过程,因为您没有自己,所以没有Self任何课程.您可以为表单类编写的类方法,例如TForm1.SendSimpleMail,在该方法内部,Self将具有TForm1实例的含义,即表单本身.
>以及因为您访问TIdSMTP类而不是对象实例而导致的其他错误.常用的做法是声明一个局部变量,创建一个对象实例,将其分配给该变量,使用该对象(变量)并释放对象实例.

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

uses
  IdSMTP,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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读