cosullivan / SmtpServer

A SMTP Server component written in C#
MIT License
676 stars 160 forks source link

SmtpCmdResp: 501 expected NOOP/RSET/QUIT/HELO/EHLO/MAIL, 4 retry(ies) remaining. #141

Closed tdhintz closed 3 years ago

tdhintz commented 3 years ago

We integrated this services into our test harnesses as a way to validate emails sent by infrastructure. It performs great with the .Net SMTP client library but when that library is replaced with Chilkat's MailMan it can no longer authenticate. The log from Chilkat says it receives a 501 response. The folks at Chilkat simply pointed to the EHLO early in the conversation as an indication that SmtpServer 'has a bug'. Any insight is appreciated.

Failed to connect to SMTP service. ChilkatLog:
  OpenSmtpConnection(109ms):
    DllDate: Feb 24 2020
    ChilkatVersion: 9.5.0.82
    UnlockPrefix: omitted
    Architecture: Little Endian; 64-bit
    Language: .NET 4.6 / x64 / VS2017
    VerboseLogging: 1
    ensureSmtpSession(109ms):
      ensureSmtpConnection(94ms):
        smtpParams:
          SmtpHost: localhost
          SmtpPort: 25
          SmtpUsername: Default
          SmtpSsl: 0
          StartTLS: 0
        --smtpParams
        smtpConnect(94ms):
          smtpHostname: localhost
          smtpPort: 25
          connectionIsReady:
            Need new SMTP connection
          --connectionIsReady
          smtpSocketConnect:
            socket2Connect:
              connect2:
                hostname: localhost
                port: 25
                ssl: False
                connectSocket:
                  domainOrIpAddress: localhost
                  port: 25
                  connectTimeoutMs: 120000
                  connect_ipv4:
                    hostname: localhost
                    port: 25
                    connectTimeoutMs: 120000
                    createSocket_ipv4:
                      Setting SO_SNDBUF size
                      sendBufSize: 262144
                      Setting SO_RCVBUF size
                      recvBufSize: 4194304
                    --createSocket_ipv4
                    connect:
                      Waiting for the connect to complete...
                      connectTimeoutMs: 120000
                      myIP: 127.0.0.1
                      myPort: 49816
                      socket connect successful.
                    --connect
                  --connect_ipv4
                --connectSocket
              --connect2
            --socket2Connect
            Turning on TCP_NODELAY.
            socketOptions:
              SO_SNDBUF: 262144
              SO_RCVBUF: 4194304
              TCP_NODELAY: 1
              SO_KEEPALIVE: 1
            --socketOptions
          --smtpSocketConnect
          smtpGreeting(31ms):
            readSmtpResponse(31ms):
              SmtpCmdResp: 220 localhost v7.2.0.0 ESMTP ready
            --readSmtpResponse
          --smtpGreeting
          ehloCommand(63ms):
            sendCmdToSmtp:
              SmtpCmdSent: EHLO MYWORKSTATION<CRLF>
            --sendCmdToSmtp
            readSmtpResponse(63ms):
              SmtpCmdResp: 250-localhost Hello MYWORKSTATION, haven't we met before?
              SmtpCmdResp: 250-PIPELINING
              SmtpCmdResp: 250-8BITMIME
              SmtpCmdResp: 250 SMTPUTF8
            --readSmtpResponse
          --ehloCommand
        --smtpConnect
      --ensureSmtpConnection
      ensureSmtpAuthenticated(15ms):
        smtpAuthenticate(15ms):
          smtp_host: localhost
          smtp_port: 25
          smtp_user: Default
          smtpAuthenticate(15ms):
            This SMTP server did not list authentication methods.
            Defaulting to LOGIN authentication method.
            login_method: LOGIN
            auth_login(15ms):
              smtpSendGet2(15ms):
                sendCmdToSmtp:
                  SmtpCmdSent: AUTH LOGIN<CRLF>
                --sendCmdToSmtp
                readSmtpResponse(15ms):
                  SmtpCmdResp: 501 expected NOOP/RSET/QUIT/HELO/EHLO/MAIL, 4 retry(ies) remaining.
                --readSmtpResponse
              --smtpSendGet2
              AUTH LOGIN failed
            --auth_login
            Failed to login using LOGIN method
          --smtpAuthenticate
          ConnectionType: Unencrypted TCP/IP
        --smtpAuthenticate
      --ensureSmtpAuthenticated
    --ensureSmtpSession
    Failed to connect to SMTP server
    Failed.
  --OpenSmtpConnection
--ChilkatLog
WaldenL commented 3 years ago

Does your builder allow unsecure authentication? Take a look at issue #120 for some sample code. I think you need to either tell the sender authentication is not needed, support encryption on the connection, or support auth on non-secure connections.

tdhintz commented 3 years ago

I didn't specify that parameter. I rewrote as follows but the symptom appears unchanged.


        /// <summary>
        /// Start receiving messages.
        /// </summary>
        public void Start()
        {
            string server;
            if (string.IsNullOrWhiteSpace(ServerName))
            {
                server = "localhost";
            }
            else
            {
                server = ServerName;
            }

            var options = new SmtpServerOptionsBuilder()
                .ServerName(server)
                .Port(Port)
                .UserAuthenticator(new UserAuthenticator())
                .MessageStore(new TestMessageStore())
                .Endpoint(builder => builder
                    .Port(Port).AllowUnsecureAuthentication(true))
                .Build();

            var smtpServer = new SmtpServer.SmtpServer(options);
            m_Receiver = smtpServer.StartAsync(m_CancelToken.Token);
        }
tdhintz commented 3 years ago

I take that back. In the rewrite note that I specified .Port() twice which caused the service to report another failure. Strange that the Chilkat symptom looked the same. Removing the first, extra, .Port() allowed it to work. Briefly, AllowUnsecureAuthentication(true) fixed this problem. Thank you!