Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing to connect due to invalid parsing of response #842

Closed
theentropic opened this issue Apr 8, 2019 · 1 comment
Closed

Failing to connect due to invalid parsing of response #842

theentropic opened this issue Apr 8, 2019 · 1 comment

Comments

@theentropic
Copy link

theentropic commented Apr 8, 2019

Describe the bug
When attempting to send mail to smtp.socketlabs.com, we get the following exception:

Status Code: 20
Error Code:UnexpectedStatusCode

Full Exception:
MailKit.Net.Smtp.SmtpCommandException: r4.us-east.aws.in.socketlabs.com Hurricane Server ESMTP service ready.
   at MailKit.Net.Smtp.SmtpClient.<ConnectAsync>d__70.MoveNext() in D:\TEMP\Versions\MailKit\MailKit\Net\Smtp\SmtpClient.cs:line 1061
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at ScratchPad.Classes.Mailer.<SendEmail>d__0.MoveNext() in D:\DEVELOPMENT\ScratchPad\ScratchPad\Classes\MailKit.cs:line 37

However, when adding logging to the server we get the correct status code response of 220.

Connected to smtps://smtp.socketlabs.com:465/
S: 220 r4.us-east.aws.in.socketlabs.com Hurricane Server ESMTP service ready.

To Reproduce

  1. Attempt to send mail using smtp.socketlabs.com over port 445
  2. On connection, see error.

Below is the code used to reproduce the behavior:

using System.Net.Mail;
using MailKit.Net;
using MailKit.Security;
using MailKit;
using MailKit.Net.Smtp;

var msg = new MailMessage();
msg.Subject = "Test Message";
msg.From = new MailAddress("test@test.com");
msg.To.Add("send@example.com");
msg.Body = "This is a test of the sending";

var opt = SecureSocketOptions.SslOnConnect;

try
{
    using (var client = new MailKit.Net.Smtp.SmtpClient(new ProtocolLogger(Console.OpenStandardOutput())))
    {
        await client.ConnectAsync("smtp.socketlabs.com", 465, opt);
        if (client.Capabilities.HasFlag(SmtpCapabilities.Authentication))
            await client.AuthenticateAsync("username", "password");
        await client.SendAsync((MimeKit.MimeMessage)msg);
        await client.DisconnectAsync(true);
    }
}
catch (SmtpCommandException e)
{
    Console.WriteLine($"Status Code: {e.StatusCode}\r\nError Code:{e.ErrorCode}\r\n\r\nFull Exception:\r\n{e}");
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}

Expected behavior
Connection should be established without issue

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser: Console App, .NET 4.6.2
  • Version 2.13

Additional context
In debugging the source code, it appears that we are receiving the response in piecemeal format (i.e. I get the "2" first, then the rest of the "20 r4.us-east.aws.in.socketlabs.com Hurricane Server ESMTP service ready."), which parses to an invalid code of 20 instead of the full 220. I believe we need to append to input when reading the response from the server

@jstedfast
Copy link
Owner

I had removed the logic to preserve that remaining input data thinking that it never would get hit (unlike in POP3 and IMAP), but I had forgotten that the leading integer code might be sent as multiple packets as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants