Search code examples
delphismtpindy10

Indy SMTP server is refusing connection


I am at the very beginning of a new project, in an area which is new to me.

I want to code an application which which will act as a middle-man Windows based email clients, such as Thunderbird, etc, and a remote SMTP server. The reason being that my application will perform some slight manipulation of emails as they pass through.

So, I figured that I will want a TIdSMTPServer. I decided to use port 6789, in case of any possible conflict (which I doubt, but ... j.i.c).

I set the SMTP server's DefaultPort to 6789, and I also bound the SMTP server to 127.0.0.1:6789 in the Bindings property (and, as @SirRufo pointed out, I set the server.active to true)..

Now, I added a button with some test code, based on this SO question. The only change I made was to change the port from SMTP.Port := 465; to SMTP.Port := IdSMTPServer.DefaultPort (host is left as 127.0.0.1).

However, when I attempt to connect my IdSMTPServer's IdSMTPServerConnect() method is never called and I get an exception, "EIdSocketError # 10061 Connection refused".

Any idea what I am doing wrong?

(and is there any guide or tutorial describing the use of IdSMTPServer?)


Solution

  • There is no problem to connect to the 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;