使用SmtpClient发送邮件的最佳方式?

All*_*est 5 .net email performance smtp smtpclient

在发送大量电子邮件时,我正在寻找良好的性能.

我听说正确的方法是打开一个连接发送~20封电子邮件并关闭连接.并一遍又一遍地这样做.它是否正确?

SmtpClient是如何工作的,是否为自己的生命周期打开了连接?(不是IDisposable,所以看起来不像那样)或者是否为每个发送的电子邮件打开连接?或者它是否一直打开连接?或者它是否有一些魔力可以在适当时打开和关闭连接?

我想知道所以我知道如何启动SmtpClient.作为单身人士或仅作为一大块信息......

dav*_*nta 7

它只从连接发送一个MailMessage.实际上,它甚至没有正确关闭连接.它发送邮件,但它不会告诉它想要退出的邮件服务器.因此,它只是让它悬空,直到底层池流决定关闭套接字.

这是Reflector的内部代码:

...
        this.GetConnection();
        fileMailWriter = this.transport.SendMail((message.Sender != null) ? message.Sender : message.From, recipients, message.BuildDeliveryStatusNotificationString(), out exception);
        }
        catch (Exception exception2)
        {
            if (Logging.On)
            {
                Logging.Exception(Logging.Web, this, "Send", exception2);
            }
            if ((exception2 is SmtpFailedRecipientException) && !((SmtpFailedRecipientException) exception2).fatal)
            {
                throw;
            }
            this.Abort();
            if (this.timedOut)
            {
                throw new SmtpException(SR.GetString("net_timeout"));
            }
            if (((exception2 is SecurityException) || (exception2 is AuthenticationException)) || (exception2 is SmtpException))
            {
                throw;
            }
            throw new SmtpException(SR.GetString("SmtpSendMailFailure"), exception2);
        }
Run Code Online (Sandbox Code Playgroud)

顺便说一下,这里是关于不发出QUIT命令的SmtpClient的更多信息. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=146711&wa=wsignin1.0

编辑:在web.archive.org上查看上面的死链接

解决方法是将SmtpClient.ServicePoint.MaxTimeout设置为1.这将更快地关闭套接字,但是,这实际上不会发出QUIT命令.