admin管理员组

文章数量:1208155

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;

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.

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;

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.
Share Improve this question edited Jan 21 at 9:02 Remy Lebeau 596k36 gold badges497 silver badges838 bronze badges asked Jan 20 at 11:21 HardLinkHardLink 111 silver badge1 bronze badge 3
  • You should NOT be calling StartSSL() AT ALL. Indy will call that internally when needed. What is more important is your configuration. What Port are you connecting to, and what do you have IMAP4.UseTLS and IdSSLIOHandlerSocketOpenSSL1.PassThrough set to? These are what influence when and how SSL is initialized on tbe connection. On a side note: you DO NOT need to set the SSLIOHandler's Destination, Host, Port, or DefaultPort properties at all. Connect() will handle that internally. – Remy Lebeau Commented Jan 20 at 17:53
  • IMAO4.Port=993; IMAP4.Destination=outlook.office365.com:993; IMAP4.UseTLS=utUseImplicitTLS; IdSSLIOHandlerSocketOpenSSL1.PassThrough=True; (False olso was try - have same result) All this parameters are work on Windows 10 ( – HardLink Commented Jan 21 at 6:38
  • The error message you are seeing is coming from the Outlook server itself. It should not be dependent on the Windows version. In any case, what is the actual IMAP command that is failing? You can get a log of IMAP commands and responses being sent by assigning a TIdLog... component to the TIdIMAP4.Intercept property. – Remy Lebeau Commented Jan 21 at 6:40
Add a comment  | 

1 Answer 1

Reset to default 1

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).

本文标签: emailDelphi 11IMAPCommand Argument Error 12 on Windows Server 2016Stack Overflow