david_navigator 12 Posted October 28, 2023 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
david_navigator 12 Posted October 28, 2023 [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 !!! 1 Share this post Link to post
dummzeuch 1505 Posted October 28, 2023 993 is the default port for Imap over TLS, so this is probably meant as a feature. (Not that I would have expected this to happen.) 1 Share this post Link to post
Remy Lebeau 1397 Posted October 28, 2023 (edited) 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 October 29, 2023 by Remy Lebeau 1 Share this post Link to post
david_navigator 12 Posted October 29, 2023 > 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
Remy Lebeau 1397 Posted October 29, 2023 (edited) 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 October 29, 2023 by Remy Lebeau Share this post Link to post
david_navigator 12 Posted October 30, 2023 >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
Remy Lebeau 1397 Posted October 30, 2023 (edited) 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 October 30, 2023 by Remy Lebeau Share this post Link to post
Remy Lebeau 1397 Posted October 30, 2023 (edited) 3 hours ago, david_navigator said: Here you go 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 October 30, 2023 by Remy Lebeau Share this post Link to post
david_navigator 12 Posted October 30, 2023 Though I can't actually change any of this as it's not my server. I just had to go with what I could to get my delphi app to talk to it. Share this post Link to post
Remy Lebeau 1397 Posted October 30, 2023 (edited) 48 minutes ago, david_navigator said: 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 October 30, 2023 by Remy Lebeau Share this post Link to post