HardLink
HardLink

Reputation: 11

Delphi 11, IMAP, Command Argument Error. 12 on Windows Server 2016

I have an application that uses Indy TIdIMAP4 to connect to a mail server, read messages, and download attachments. The app works correctly on Windows 10.

When trying to transfer it to Windows 11 or Windows Server 2016, I get the error:

Command Argument Error. 12

I'm using the latest version of the SSL libraries downloaded from GitHub. With the same version, another application works fine, which only sends emails.

Here's the code:

var
  IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
  IMAP4: TIdIMAP4;
...
begin
  IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create(IMAP4);
  IdSSLIOHandlerSocketOpenSSL1.Destination := IMAP4.Host + ':' + IntToStr(IMAP4.Port);
  IdSSLIOHandlerSocketOpenSSL1.Host := IMAP4.Host;
  IdSSLIOHandlerSocketOpenSSL1.Port := IMAP4.Port;
  IdSSLIOHandlerSocketOpenSSL1.DefaultPort := IMAP4.Port;
  IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvTLSv1_2;
  IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Mode := sslmUnassigned;
  IMAP4.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
for I := 1 to 10 do
      begin
        try
          IMAP4.Connect; 
          if IMAP4.Connected then
            Break;
        except
          on E: Exception do
            AddLog('Error: ' + IntToStr(I) + '. ' + E.Message, False, True);
        end;
      end;
end;

Version SSL

If you add SSLHandler.StartSSL before a Connect call then the error "Socket Error #10038" will appear.

Can you help me figure out why I can't connect to the mail server?

I tried using different versions of the SSL libraries, calling the SSLHandler.StartSSL method before joining. Nothing helped.


UPDATE:

Here is a log of the connection attempt that fails:

Stat Connected.
Recv 21.01.2025 10:36:24: * OK The Microsoft Exchange IMAP4 service is ready. [TQBNADAAUAAyADgAMABDAEEAMAAxADAAMwAuAFMAVwBFAFAAMgA4ADAALgBQAFIATwBEAC4ATwBVAFQATABPAE8ASwAuAEMATwBNAA==]
Sent 21.01.2025 10:36:24: C1 LOGIN 
Recv 21.01.2025 10:36:24: C1 BAD Command Argument Error. 12
Sent 21.01.2025 10:36:24: C2 LOGOUT
Recv 21.01.2025 10:36:24: * BYE Microsoft Exchange Server IMAP4 server signing off.
C2 OK LOGOUT completed.
Stat Disconnected.

Upvotes: 1

Views: 67

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595295

The server is reporting the error message in reply to a LOGIN command that is not sending any credentials.

The only way that can happen is if you have IMAP4.AuthType set to iatUserPass (the default) but both IMAP4.Username and IMAP4.Password are blank. Don't do that!

You need to either:

  • assign valid credentials before calling IMAP4.Connect().

  • set the AAutoLogin parameter of IMAP4.Connect() to False (it is True by default). You can call IMAP4.Login() afterwards if needed (after assigning valid credentials).

Upvotes: 1

Related Questions