.NET System.Net.Mail.SmtpClient class doesn't

2019-03-30 14:30发布

问题:

Has anybody had issues with this? If so, how do you get around it? We are getting sporadic timeout issues and this is getting blamed.

The same issue is reported here as well:

http://www.vbforums.com/showthread.php?p=3609268

回答1:

I don't know if there's an easier way to work around this specific problem, but one option would be to download the source for Mono's SmtpClient and use that (modifying if necessary). Their version definitely does send a QUIT command.

One project that I work on required us to send large numbers of emails. .NET's implementation was too inefficient, not providing any way to send multiple distinct emails in the same SMTP session. We fell back on using Mono's implementation and modifying it to allow us to manually control when the QUIT command was sent and the connection closed. There were a total of 25 relevant Mono classes that we had to merge into our project for this (mostly copy+paste and edit namespace).

It seems a little extreme, but if there's no way to work around the issue, it may be your best 3rd-party alternative: it's free, it's not a great deal of work and its API is almost identical to the native SmtpClient's.



回答2:

Is your application running on a machine with an IIS? If so, you could take advantage of the built-in SMTP Service by setting the delivery method of your SMTP client like this:

var client = new SmtpClient 
{ 
    DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis 
};

I am using this in a few applications, and it's very reliable.



回答3:

It is solved in .Net 4. They implemented IDispose and this sends the QUIT command and frees resources.

This is a copy of the relevant documentation on MSDN:

The SmtpClient class has no Finalize method, so an application must call Dispose to explicitly free up resources. The Dispose method iterates through all established connections to the SMTP server specified in the Host property and sends a QUIT message followed by gracefully ending the TCP connection. The Dispose method also releases the unmanaged resources used by the Socket and optionally disposes of the managed resources.

Call Dispose when you are finished using the SmtpClient. The Dispose method leaves the SmtpClient in an unusable state. After calling Dispose, you must release all references to the SmtpClient so the garbage collector can reclaim the memory that the SmtpClient was occupying.



回答4:

Looks like I'm going to find a 3rd party product to fix the problem. The mono solution sounds interesting, but I'd rather spend a few bucks and get a commercial solution.



标签: .net smtp