Jump to content
david_navigator

Code using TIdIMAP4 driving me mad !!

Recommended Posts

I have the following fairly simple code to connect to my IMAP server, however I'm doing something wrong, but I can't work out what.

The first time I click the button, I always get 

ExceptionMessage="Socket Error # 10061
Connection refused."
ExceptionName="EIdSocketError"

However if I then click on the button again, the IMAP control connects to my server just fine and I can retrieve anything I like without a problem.

 

I've tried setting various properties in various different orders, but that makes no difference, so why does it not work the first time ?

 

If I make TheImap &  IdSSLIOHandlerSocketOpenSSL1 local to the button1click procedure, rather than class level, then TheIMAP never connects, so that seems to rule out something external blocking the connection first time round and points to something not being initialized correctly, but what am I missing ?

 

type
  TForm3 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    TheImap: TIdIMAP4;
    IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.FormCreate(Sender: TObject);
begin
  TheImap := TIdIMAP4.Create(nil);
  IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create(TheImap);
end;

procedure TForm3.Button1Click(Sender: TObject);

begin
  if TheImap.connected then TheImap.Disconnect;

  TheImap.Host := 'xxxxxxxxx';
  TheImap.Username := 'xxxxxxx';
  TheImap.Password := 'xxxxxxx';
  TheImap.Port := 143;

  with IdSSLIOHandlerSocketOpenSSL1 do
  begin
    Destination := format('%s:%d', [TheImap.Host, TheImap.Port]); // e.g 'imap.gmail.com:993';
    Host := TheImap.Host;
    MaxLineAction := maException;
    Port := TheImap.Port;
    DefaultPort := 0;
    SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
    SSLOptions.Mode := sslmUnassigned;
    SSLOptions.VerifyMode := [];
    SSLOptions.VerifyDepth := 0;
  end;
  TheImap.IOHandler := IdSSLIOHandlerSocketOpenSSL1;

  TheImap.UseTLS := utUseImplicitTLS;

  try
    TheImap.Connect;
  except
    on E: exception do
      showmessage(E.Message);
  end;
  if TheImap.Connected then
    showmessage('Connected ok');
end;

 

Share this post


Link to post

[Some time later]

 

I've now discovered that the IdSSLIOHandlerSocketOpenSSL1 code changes the TIMAP4.port property to 993, even though my server wants 143, so setting

TheImap.Port := 143;

after setting the IdSSLIOHandlerSocketOpenSSL1 properties, addresses the issue. 

Not sure if this is an Indy bug or a misunderstanding of how it should work by me.

 

Replying to my own question simply because this will no doubt bite me on the bum in 5 years time when I refactor the code !!!

  • Like 1

Share this post


Link to post
On 10/28/2023 at 1:57 AM, david_navigator said:

[Some time later]

 

I've now discovered that the IdSSLIOHandlerSocketOpenSSL1 code changes the TIMAP4.port property to 993, even though my server wants 143, so setting


TheImap.Port := 143;

after setting the IdSSLIOHandlerSocketOpenSSL1 properties, addresses the issue. 

Not sure if this is an Indy bug or a misunderstanding of how it should work by me.

It is not a bug, and it is not the SSLIOHandler that is changing the Port.  It is actually the UseTLS property setter that does it.  You are initializing the Port to the standard IMAP port 143, then setting UseTLS to utUseImplicitTLS, so the property setter changes the Port to the standard IMAP implicit TLS port 993.  This is working as designed.

 

If you need to use implicit TLS on port 143 (why?), then you need to set the Port after setting the UseTLS.

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post

need to use implicit tls on port 143 (why?), then you need to set the Port after setting the UseTLS.

Thanks.

 

No idea why. I didn't configure the server :)

Share this post


Link to post

Are you sure you are supposed to be using implicit TLS on port 143, and not explicit TLS instead?

 

Implicit TLS sends the TLS handshake as soon as the TCP connection is established, before exchanging any other data, thus all protocol data is encrypted.

 

Explicit TLS (aka STARTTLS) sends the TLS handshake after first establishing an unencrypted TCP connection and greeting the server, and then asking the server for permission to start a TLS session (such as before authenticating).

 

For IMAP, implicit TLS is typically used on port 993, and explicit TLS is used on port 143.

Edited by Remy Lebeau

Share this post


Link to post

>Are you sure you are supposed to be using implicit TLS on port 143, and not explicit TLS instead?

If I change the test code above to use 
explicit then

TheImap.Connect;

never returns. Have I missed setting something else or if this just a strange server ?

 

 

Share this post


Link to post
8 hours ago, david_navigator said:

Have I missed setting something else or if this just a strange server ?

Can you please provide the actual configuration you are expected to use?  This does not sound like a standard setup.

Edited by Remy Lebeau

Share this post


Link to post
3 hours ago, david_navigator said:

Here you go

 

image.thumb.png.cce0dba97ee956d3678998361867fd53.png

That basically says to use implicit SSL on port 993, and use port 143 for everything else.  Which matches up with what I described earlier.

 

What are the other options provided in the "Encryption" field?

Edited by Remy Lebeau

Share this post


Link to post
48 minutes ago, david_navigator said:

image.png.933d3d133f502a942b1a1b664ff25da6.png

In that case, the "SSL" option is for implicit SSL/TLS (port 993), and the "STARTTLS" option is for explicit TLS (port 143), where "None/STARTTLS" makes TLS optional whereas the other "STARTTLS" option makes TLS required.

 

So it looks like the server is using a typical IMAP setup, you just had a mismatch in your code.

Edited by Remy Lebeau

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×