jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.
http://www.mimekit.net
MIT License
6.19k stars 821 forks source link

[MacOSX] SmtpCommandException: ITMIME #666

Closed ghost closed 6 years ago

ghost commented 6 years ago

Trying to send a sample email to a local SMTP server called Smtp4dev on Mac OS X 10.13.3 with .Net Core 2.0.

Gets to the part where it tries to send FROM: information, and I believe the issue could be centered around carriage / newline characters.

Sample code:

var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey Tribbiani", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Mrs. Chanandler Bong", "chandler@friends.com"));
message.Subject = "How you doin'?";

message.Body = new TextPart ("plain") {
    Text = @"Hey Chandler,

    I just wanted to let you know that Monica and I were going to go play some paintball, you in?

    -- Joey"
};

using (var client = new SmtpClient (new ProtocolLogger ("smtp.log"))) {
    // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
    client.ServerCertificateValidationCallback = (s,c,h,e) => true;

    client.Connect ("localhost", 2525, false);

    client.Send (message);
    client.Disconnect (true);
}

Reason I posted up this sample email is because it wasn't working with my company's app on my computer, but a coworker had the same project working fine on windows. So I launched up this sample program using the same Mailkit package and received the exact error here.

Thought it could be because of Smtp4dev but I can load up a MailMessage() email and send using System.Net.Mail.SmtpClient and that works fine with Smtp4dev.

Unhandled Exception: MailKit.Net.Smtp.SmtpCommandException: ITMIME
   at MailKit.Net.Smtp.SmtpClient.ProcessMailFromResponse(MimeMessage message, MailboxAddress mailbox, SmtpResponse response)
   at MailKit.Net.Smtp.SmtpClient.<MailFromAsync>d__88.MoveNext()
--- 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 MailKit.Net.Smtp.SmtpClient.<SendAsync>d__99.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at MailKit.Net.Smtp.SmtpClient.<SendAsync>d__99.MoveNext()
--- 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 MailKit.Net.Smtp.SmtpClient.Send(FormatOptions options, MimeMessage message, CancellationToken cancellationToken, ITransferProgress progress)
   at MailKit.MailTransport.Send(MimeMessage message, CancellationToken cancellationToken, ITransferProgress progress)
   at ConsoleApp1.Program.Main(String[] args) in /Users/anthonyronning/Dev/Projects/TestEmail/ConsoleApp1/ConsoleApp1/Program.cs:line 55

Line 55 is client.Send (message);

Below is the SMTP log:

Connected to smtp://localhost:2525/?starttls=when-available
S: 220 ip-***-***-***-*** smtp4dev ready
C: EHLO [127.0.0.1]
S: 250 Nice to meet you.
S: 8BITMIME
S: SIZE
C: MAIL FROM:<joey@friends.com>
C: RSET

The reponse coming back after the MAIL FROM is ITMIME with a code of 8. Not sure where this comes from, doesn't look like something called out anywhere in Smtp4dev code.

jstedfast commented 6 years ago

The problem is with Smtp4dev which is sending invalid responses.

Note the 8BITMIME line. That's the problem right there. The reason it's a problem is because it is supposed to start with a SMTP response code, but it doesn't.

The previous line (the response to the EHLO command) should also be changed and the entire EHLO response should look like this:

250-Nice to meet you.
250-8BITMIME
250 SIZE

The - character immediately following the 250 means that there is at least 1 more line of the response that the client needs to read.

File a bug against Smtp4dev.