2012-06-22 135 views
7

我嘗試發送電子郵件,但我有一個問題,但是,我在網上找到了這段代碼:SendEmail與Indy組件

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 := '[email protected]'; 
Msg.Subject := 'Test'; 

DestAddr := Msg.Recipients.Add; 
DestAddr.Text := 'name'; 
DestAddr.Address := '[email protected]'; 
Msg.Body.Add('simple test mail.'); 

tIdSMTP.Host := 'smtp.gmail.com'; 
tIdSMTP.Port := 25; 
tIdSMTP.AuthenticationType := atLogin; //error here (2 error) 
tIdSMTP.Username := '[email protected]'; 
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' 

感謝您的幫助提前

+1

有一件事是加入'IdEMailAddress'你使用條款,以使編譯器知道'TIdEmailAddressItem' ,但另一個是,這個例子基本上是錯誤的,它是因爲Indy 9(因爲atLogin認證類型),你使用什麼版本的Indy?你可以檢查,如果你按住'CTRL'鍵並點擊例如你的使用條款中的'IdSMTP',然後檢查'IdSMTP.pas'的存儲位置。如果它在'Indy9'或'Indy10'文件夾中。 – TLama

+0

好的謝謝,現在錯誤減少,但現在是3錯誤(我編輯了第一篇文章),我有Indy9和Indy10,但是當我點擊idSMTP我得到一個錯誤:無法找到文件idSMTP.pas –

回答

8

從你的問題的代碼爲印第安納波利斯9和您的編譯器錯誤書面似乎你使用印10.爲了你的編譯器錯誤:

  • Undeclared identifier: Self - 的Self是指針類實例本身並且因爲您沒有將SendSimpleMail作爲類方法編寫,而只是作爲獨立程序使用,所以您沒有任何Self,因爲您沒有任何類。您可以爲例如您的表單類編寫的類方法,例如TForm1.SendSimpleMail,在該方法中,Self將具有TForm1實例的含義,該形式本身。

  • 而你得到的其他錯誤是因爲你正在訪問TIdSMTP類,而不是對象實例。常用的做法是聲明一個局部變量,創建一個將其分配給該變量的對象實例,使用該對象(變量)並釋放該對象實例。

我會嘗試這樣的事情(使用附帶德爾福2009年印第安納波利斯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 := '[email protected]'; 
    IdSMTP.Password := 'password'; 
    IdSMTP.Connect; 
    if IdSMTP.Authenticate then 
    begin 
     IdMessage := TIdMessage.Create(nil); 
     try 
     IdMessage.From.Name := 'User Name'; 
     IdMessage.From.Address := '[email protected]'; 
     IdMessage.Subject := 'E-mail subject'; 
     IdMessage.Body.Add('E-mail body.'); 

     IdEmailAddressItem := IdMessage.Recipients.Add; 
     IdEmailAddressItem.Address := '[email protected]'; 

     IdSMTP.Send(IdMessage); 
     finally 
     IdMessage.Free; 
     end; 
    end; 
    IdSMTP.Disconnect; 
    finally 
    IdSMTP.Free; 
    end; 
end; 
+0

謝謝,但現在我有最後一個錯誤:[DCC錯誤] Unit1.pas(42):E2003未聲明的標識符:'satDefault' –

+2

我不確定這一點,因爲在['reference'](http:// www.indyproject.org/docsite/html/TIdSMTP_AuthType.html)有'atDefault',但在Delphi 2009附帶的版本中是'satDefault'。所以請嘗試使用'atDefault'。 – TLama

+1

謝謝工作! :D –