有什么办法可以关闭邮件 smtp 会话吗?

发布于 2024-11-06 14:46:47 字数 661 浏览 1 评论 0原文

我正在使用 Gmail STMP 服务器发送电子邮件。它工作得很好。但几天以来,它有时会停止工作。现在,它只能工作十分之五。

异常:发送电子邮件失败

内部异常:无法连接到远程服务器。

与托管技术支持交谈后,他们说他们的服务器上存在邮件会话限制。这是一个“共享主机”,因此当它超过时,所有新连接都会被阻止。他们说他们正在努力解决这个问题。但也表示请检查您是否“正确关闭了邮件会话”。

我查看了它,但没有 Close()Dispose()。我还读到 SMTP 传输没有确认?

请告诉我是否有办法关闭邮件会话?或者任何解决此问题的解决方法。

我正在使用System.Net.Mail

MailMessage msg = new MailMessage();
SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential info = new NetworkCredential("email", "password");

然后还有另一种方法调用sc.Send()

I am using Gmail STMP server to send emails. It works just fine. But since few days, it sometimes stops working. Now, it is only working 5 out of 10 times.

Exception: Failure Sending Email

Inner Exception: Unable to connect to remote server.

After talking to hosting technical support, they said there is a mail session limit on their server. This is a "Shared Hosting", so when it exceeds all new connections are blocking. They said they are trying to fix it. But also said please check that you are "closing the mail session properly or not".

I looked into it, but there is no Close() or Dispose(). I also read there is no acknowledgement for SMTP tranfer?

Please let me know if there is anyway to close the mail session? Or any workaround to fix this issue.

I am using System.Net.Mail:

MailMessage msg = new MailMessage();
SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential info = new NetworkCredential("email", "password");

Then there is another method which calls sc.Send().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落画卷 2024-11-13 14:46:47

System.Net.Mail.SmtpClient< /code>实现了 IDisposable,所以我建议你使用它。使用 使用 块以确保它得到正确处理。

请特别注意,不建议使用 System.Web.Mail,而改用 System.Net.Mail

using (SmtpClient client = new SmtpClient("mail.google.com")) 
{

}

您正在使用System.Net.Mail。在这种情况下,您会发现 SMTPClient 确实有一个 Dispose 方法(因为它实现了 IDisposable),它将正常关闭 SMTP 连接。但是,使用 using 块被认为是更好的做法,而不是直接调用 Dispose。最后,请注意链接文档中的以下内容:

SmtpClient类没有Finalize
方法。所以应用程序必须调用
处置以显式释放
资源。

Dispose方法迭代
所有已建立的连接
Host 中指定的 SMTP 服务器
属性并发送 QUIT 消息
然后优雅地结束 TCP
联系。 Dispose 方法还
释放使用的非托管资源
通过底层 Socket。

The System.Net.Mail.SmtpClient implements IDisposable, so I would suggest you use that. Use a using block to ensure it gets correctly disposed of.

Note particularly that use of System.Web.Mail is deprecated in favour of System.Net.Mail.

using (SmtpClient client = new SmtpClient("mail.google.com")) 
{

}

You are using System.Net.Mail. In that case, you will find that the SMTPClient does have a Dispose method (as it implements IDisposable), which will gracefully close the SMTP connection. However, it is considered better practice to use a using block, rather than calling Dispose directly. Finally, please note the following from the linked documentation:

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 underlying Socket.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文