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

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. #921

Closed
djrpascu opened this issue Oct 2, 2019 · 21 comments
Labels
compatibility Compatibility with existing software

Comments

@djrpascu
Copy link

djrpascu commented Oct 2, 2019

I get the following error message when trying to send a simple message:

A connection attempt failed because the connected party did not properly respond after a
period of time, or established connection failed because connected host has failed to respond.

I have a simple console app that's doing this:

var message = new MimeMessage();
message.To.Add(new MailboxAddress("test", "user@server.com"));
message.From.Add(new MailboxAddress("do-not-reply", "do-not-reply@server.com"));
message.Subject = "TEST";

var mailkitClient = new SmtpClient();
mailkitClient.Connect("smtp.server.net", 25, false);
mailkitClient.Send(message);

It hangs when trying to call Send and comes back with the error message above.

If I use the regulat .NET SmtpClient I have no problems running the following and getting an email:

var smtpClient = new SmtpClient();
smtpClient.Host = "smtp.server.net";
smtpClient.Port = 25;

await smtpClient.SendMailAsync("donotreply@server.com", "user@server.com", "test", "test");

So I don't believe I have a firewall or port issue. What am I missing?

Trying this from a console app with .NET Core 3.0. Also tried from a .NET Standard 2.0 class library project. Using MailKit 2.3.1.6.

@jstedfast
Copy link
Owner

That sounds like an error you would get in SmtpClient.Connect(), not SmtpClient.Send(). Are you sure you're getting that in Send()?

Also, that's an error that gets thrown from Socket.Connect() (or, in MailKit's case, Socket.Begin/EndConnect() since it uses the async API).

There have been a number of reports like this but every person who has reported this has eventually discovered that they had networking problems and/or had Avast! antivirus installed which has a firewall.

Do you have Avast! installed? Any other virus scanner that has a firewall?

@djrpascu
Copy link
Author

djrpascu commented Oct 2, 2019

it seems to be happening in the Send. I am able to step over the Connect method without problem. After stepping over Send it hangs for like a minute or 2 before dropping into my catch. I discovered the ProtocolLogger when searching through other issues. Here's my results:

Connected to smtp://smtp.server.net:25/?starttls=when-available
S: 220 HUB01B.server.net Microsoft ESMTP MAIL Service ready at Tue, 1 Oct 2019 16:11:40 -1000
C: EHLO [127.0.0.1]
S: 250-HUB01B.server.net Hello [172.30.192.164]
S: 250-SIZE 25600000
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-AUTH
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250-XEXCH50
S: 250 XSHADOW
C: MAIL FROM:<do-not-reply@server.com> SIZE=710
C: RCPT TO:<user@server.com>
S: 250 2.1.0 Sender OK
S: 250 2.1.5 Recipient OK
C: BDAT 710 LAST
C: From: do-not-reply <do-not-reply@server.com>
C: Date: Tue, 01 Oct 2019 16:11:31 -1000
C: Subject: test
C: Message-Id: <ZVAU9RWAP8U4.4G31NCNJMBT53@localhost.localdomain>
C: To: test <user@server.com>
C: MIME-Version: 1.0
C: Content-Type: multipart/alternative; boundary="=-lk0PxVMEJOicw2ZlhdNjVQ=="
C: 
C: --=-lk0PxVMEJOicw2ZlhdNjVQ==
C: Content-Type: text/plain; charset=utf-8
C: 
C: Here's the boring plain text version!
C: --=-lk0PxVMEJOicw2ZlhdNjVQ==
C: Content-Type: text/html; charset=utf-8
C: Content-Id: <B6SJTRWAP8U4.EFQAKOAQECX13@localhost.localdomain>
C: 
C: <br /><h2>This is the header</h2><p>Here's a paragraph with some <b>bold</b> text</p><br><br>Bye
C: --=-lk0PxVMEJOicw2ZlhdNjVQ==--

@djrpascu
Copy link
Author

djrpascu commented Oct 2, 2019

regarding antivirus, i have Symantec Endpoint Protection. i don't see anything blocked in there. also, when using the System.Net.Mail SmtpClient, that doesn't get blocked and works fine.. would it be safe to assume MailKit SmtpClient wouldn't get blocked as well?

@jstedfast
Copy link
Owner

n/m, this isn't a firewall issue based on your log or you wouldn't be able to connect at all.

@jstedfast
Copy link
Owner

Can you try using MailKit 2.2.0?

@jstedfast
Copy link
Owner

or... better yet, just try this:

...
client.Connect (...);
client.Capabilities &= ~SmtpCapabilities.Chunking;
client.Capabilities &= ~SmtpCapabilities.Binary;
client.Send (...);

@djrpascu
Copy link
Author

djrpascu commented Oct 2, 2019

2.20 worked

AND

the following worked on Mailkit 2.3.1.6 as well!

mailkitClient.Capabilities &= ~SmtpCapabilities.Chunking;
mailkitClient.Capabilities &= ~SmtpCapabilities.BinaryMime;

much mahalo!

@jstedfast
Copy link
Owner

Curious what the following code snippet produces for you:

using (var memory = new MemoryStream ()) {
    var options = FormatOptions.Default.Clone ();
    options.NewLineFormat = NewLineFormat.Dos;
    options.EnsureNewLine = true;

    message.WriteTo (options, memory);
    Console.WriteLine ("memory.Length = {0}", memory.Length);
}

@djrpascu
Copy link
Author

djrpascu commented Oct 2, 2019

memory.Length = 710

@jstedfast
Copy link
Owner

wtf, seems like a bug in Exchange not properly counting the bytes it has received??

Does the stream end with a newline sequence?

using (var memory = new MemoryStream ()) {
    var options = FormatOptions.Default.Clone ();
    options.NewLineFormat = NewLineFormat.Dos;
    options.EnsureNewLine = true;

    message.WriteTo (options, memory);

    var buffer = memory.ToArray ();
    var eoln = buffer[buffer.Length - 2] == '\r' && buffer[buffer.Length - 1] == '\n';

    Console.WriteLine ("buffer length = {0}, ends with newline = {1}", buffer.Length, eoln);
}

@djrpascu
Copy link
Author

djrpascu commented Oct 2, 2019

i think we're on Exchange Server 2016

buffer length = 710, ends with newline = True

@jstedfast
Copy link
Owner

jstedfast commented Oct 2, 2019

In case it's not obvious what I'm lookin at, I'm verifying that the BDAT command is correct and that the content following the BDAT command is the correct number of bytes and that the end of the BDAT message data ends with a newline sequence that should all force Exchange to recognize that it has received the full message.

What appears to be happening is that MailKit's SmtpClient is completing the BDAT command (including the payload) but the Exchange server is still waiting for more data. This creates a situation where both MailKit and Exchange are waiting for the other to say something but neither says anything thus leading to an eventual connection timeout (roughly 2 minutes or so?)

@jstedfast
Copy link
Owner

You've got MimeKit 2.3.1, right? I'm pretty sure MailKit 2.3.1.6 pulls in MimeKit 2.3.1 so you should have it, but just to verify... (MimeKit 2.3.1 has an essential fix for BDAT to work properly).

@jstedfast
Copy link
Owner

Anyway, time for me to go to bed, but I'll think about this more tomorrow.

@djrpascu
Copy link
Author

djrpascu commented Oct 2, 2019

I just brought in MailKit 2.3.1.6 via NuGet. If I check Packages > MailKit > MimeKit it states (2.3.1).

Thanks for the quick replies, and for the awesome library! Good night! Let me know if you want me to try anything else. Thanks again!

@jstedfast jstedfast added the compatibility Compatibility with existing software label Oct 2, 2019
@jstedfast
Copy link
Owner

This should solve the issue by using the DATA command instead of BDAT.

IMHO, this is actually a bug in Exchange, but this fix is all I can do.

@djrpascu
Copy link
Author

djrpascu commented Oct 3, 2019

Thanks!

@nitinag
Copy link
Sponsor

nitinag commented Nov 24, 2019

@jstedfast Since BDAT was taken out for this potential case, is it possible to add the ability to control / opt-in to the BDAT behavior (if supported) on a SmtpClient session?

@jstedfast
Copy link
Owner

@nitinag Would this work for you? 2b2cacf

@nitinag
Copy link
Sponsor

nitinag commented Nov 24, 2019

Yes, that'll work. Thanks!

@thewitcher7667
Copy link

This worked for me thank you very much

2.20 worked

AND

the following worked on Mailkit 2.3.1.6 as well!

mailkitClient.Capabilities &= ~SmtpCapabilities.Chunking;
mailkitClient.Capabilities &= ~SmtpCapabilities.BinaryMime;

much mahalo!

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

No branches or pull requests

4 participants