2014-04-11 186 views
-1

我正處於一個新項目的開始階段,對我來說這是一個新的領域。Indy SMTP服務器拒絕連接

我想編寫一個應用程序,它將充當基於Windows的中間人電子郵件客戶端,如Thunderbird等,以及遠程SMTP服務器。原因是我的應用程序會在電子郵件通過時對其進行輕微操作。我想我會想要一個TIdSMTPServer。我決定使用端口6789,以防發生任何可能的衝突(我懷疑,但是... j.i.c)。

我設置SMTP服務器的DefaultPort到6789,我在Bindings屬性也必然SMTP服務器127.0.0.1:6789(和,作爲@SirRufo指出的那樣,我設置了server.active爲true)..

現在,我添加了一個帶有一些測試代碼的按鈕,基於this SO question。我做的唯一更改是將端口從SMTP.Port := 465;更改爲SMTP.Port := IdSMTPServer.DefaultPort(主機保留爲127.0.0.1)。

但是,當我試圖連接我的IdSMTPServerIdSMTPServerConnect()方法從不被調用,我得到一個異常,「EIdSocketError#10061連接被拒絕」。

任何想法我做錯了什麼?

(並且沒有任何指南或教程描述使用IdSMTPServer的?)

+2

問題你激活服務器? 'IdSMTPServer1.Active:= True' –

回答

2

沒有連接到TIdSMTPServer

type 
    TForm1 = class(TForm) 
    IdSMTP1 : TIdSMTP; 
    IdSMTPServer1 : TIdSMTPServer; 
    Button1 : TButton; 
    ListBox1 : TListBox; 
    procedure Button1Click(Sender : TObject); 
    procedure IdSMTPServer1Connect(AContext : TIdContext); 
    procedure IdSMTPServer1Disconnect(AContext : TIdContext); 
    private 
    procedure Log(const AMsg : string); 
    public 
    { Public-Deklarationen } 
    end; 

var 
    Form1 : TForm1; 

implementation 

{$R *.dfm} 

procedure TForm1.Button1Click(Sender : TObject); 
begin 
    // Server Settings 
    IdSMTPServer1.DefaultPort := 6728; 
    IdSMTPServer1.OnConnect := IdSMTPServer1Connect; 
    IdSMTPServer1.OnDisconnect := IdSMTPServer1Disconnect; 
    // Client Settings 
    IdSMTP1.Host := '127.0.0.1'; 
    IdSMTP1.Port := IdSMTPServer1.DefaultPort; 
    // Connect Client to Server 
    IdSMTPServer1.Active := True; 
    try 
    IdSMTP1.Connect; 
    IdSMTP1.Disconnect(True); 
    finally 
    IdSMTPServer1.Active := False; 
    end; 
end; 

procedure TForm1.Log(const AMsg : string); 
begin 
    if MainThreadID = TThread.CurrentThread.ThreadID 
    then 
    begin 
    ListBox1.ItemIndex := ListBox1.Items.Add(AMsg); 
    end 
    else 
    TThread.Queue(nil, 
     procedure 
     begin 
     Log(AMsg) 
     end); 
end; 

procedure TForm1.IdSMTPServer1Connect(AContext : TIdContext); 
begin 
    Log('Connect'); 
end; 

procedure TForm1.IdSMTPServer1Disconnect(AContext : TIdContext); 
begin 
    Log('Disconnect'); 
end; 
+0

感謝1,000,000。你的代碼看起來很棒,所以我一行一行地瀏覽了我的代碼,看到我編寫了'TestIdSMTP.Host:='127.0.1.1'; // SMTP服務器的IP地址'仔細閱讀,特別是第三部分Grrrr。感謝您讓我減速並檢查它! – Mawg

+1

不客氣:o) –