MailKit:使用 Client.Disconnect(...) 的最佳做法是什么
MailKit: What is the best practice of using Client.Disconnect(...)
我最近在我们的项目中使用了 Mailkit 库来替换 .NET SmtpClient。
我们有 2 个使用 SmtpClient 发送电子邮件的业务案例。
在一个实例中,我们使用 SmtpClient 在单独的进程中发送排队的电子邮件,而在另一个实例中,我们立即发送电子邮件。
在实现时我注意到我们必须调用 Client 实例的 Disconnect 方法。
<code>mailClient.Disconnect(...);
不确定也不清楚文档中调用此方法的最佳方式是什么。
所以我的问题是,使用这种方法的最佳做法是什么?
每条消息调用 mailClient.Disconnect(true) 或 mailClient.Disconnect(false)?
有趣的是,如果我在 using 块中使用客户端,我是否需要在发送消息后显式调用 Disconnect(...) ?我认为它在执行 Dispose() 时隐式调用断开连接。
<code>using (var mailClient = new SmtpClient())
{
mailClient.Connect(...);
mailClient.AuthenticationMechanisms.Remove("XOAUTH2");
mailClient.Authenticate(...);
mailClient.Send(message);
mailClient.Disconnect(false);
}
感谢您在这方面的反馈。
Dispose()
方法只会关闭还活着的套接字连接(这实际上与调用 Disconnect (false)
相同)。
不过,调用 Disconnect (true)
要礼貌得多,因为它会向服务器发送适当的 LOGOUT
或 QUIT
命令,使服务器能够正确处理其资源。
我最近在我们的项目中使用了 Mailkit 库来替换 .NET SmtpClient。
我们有 2 个使用 SmtpClient 发送电子邮件的业务案例。
在一个实例中,我们使用 SmtpClient 在单独的进程中发送排队的电子邮件,而在另一个实例中,我们立即发送电子邮件。
在实现时我注意到我们必须调用 Client 实例的 Disconnect 方法。
<code>mailClient.Disconnect(...);
不确定也不清楚文档中调用此方法的最佳方式是什么。
所以我的问题是,使用这种方法的最佳做法是什么?
每条消息调用 mailClient.Disconnect(true) 或 mailClient.Disconnect(false)?
有趣的是,如果我在 using 块中使用客户端,我是否需要在发送消息后显式调用 Disconnect(...) ?我认为它在执行 Dispose() 时隐式调用断开连接。
<code>using (var mailClient = new SmtpClient())
{
mailClient.Connect(...);
mailClient.AuthenticationMechanisms.Remove("XOAUTH2");
mailClient.Authenticate(...);
mailClient.Send(message);
mailClient.Disconnect(false);
}
感谢您在这方面的反馈。
Dispose()
方法只会关闭还活着的套接字连接(这实际上与调用 Disconnect (false)
相同)。
不过,调用 Disconnect (true)
要礼貌得多,因为它会向服务器发送适当的 LOGOUT
或 QUIT
命令,使服务器能够正确处理其资源。