申请关闭之前不会发送电子邮件
我有一个使用 SmtpClient 发送电子邮件的应用程序,但直到应用程序关闭后才会发送电子邮件。我一直在寻找解决问题的方法,但我找不到。
系统确实安装了赛门铁克防病毒软件,这可能是问题所在。
有人有解决这个问题的办法吗?
这是我正在使用的代码。
public class EMail
{
private string server;
public string Server {get{return this.server;}set{this.server = value;}}
private string to;
public string To {get{return this.to;}set{this.to = value;}}
private string from;
public string From {get{return this.from;}set{this.from = value;}}
private string subject;
public string Subject {get{return this.subject;}set{this.subject = value;}}
private string body;
public string Body {get{return this.body;}set{this.body = value;}}
public EMail()
{}
public EMail(string _server, string _to, string _from, string _subject, string _body)
{
this.Server = _server;
this.To = _to;
this.From = _from;
this.Subject = _subject;
this.Body = _body;
}
public void Send()
{
using(System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(this.From, this.To, this.Subject, this.Body))
{
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(this.Server);
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
//I have tried this, but it still does not work.
//client.ServicePoint.ConnectionLeaseTimeout = 0;
try
{
client.Send(message);
}
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
}
}
}
编辑:
事实证明,电子邮件最终在 2-3 分钟后发送。看起来好像正在由交换服务器排队,或者 SmtpClient 连接最终超时并被服务器关闭。
编辑:
我已经尝试过。
client.ServicePoint.ConnectionLeaseTimeout = 1;
client.ServicePoint.MaxIdleTime = 1;
I have an application that uses SmtpClient to send E-Mail, but the E-Mails are not sent until the application closes. I have searched and searched to find a solution to the problem, but I am not able to find one.
The system does have Symantec anti-virus installed, which could possibly be the problem.
Does anybody have a solution to this problem?
Here is the code I am using.
public class EMail
{
private string server;
public string Server {get{return this.server;}set{this.server = value;}}
private string to;
public string To {get{return this.to;}set{this.to = value;}}
private string from;
public string From {get{return this.from;}set{this.from = value;}}
private string subject;
public string Subject {get{return this.subject;}set{this.subject = value;}}
private string body;
public string Body {get{return this.body;}set{this.body = value;}}
public EMail()
{}
public EMail(string _server, string _to, string _from, string _subject, string _body)
{
this.Server = _server;
this.To = _to;
this.From = _from;
this.Subject = _subject;
this.Body = _body;
}
public void Send()
{
using(System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(this.From, this.To, this.Subject, this.Body))
{
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(this.Server);
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
//I have tried this, but it still does not work.
//client.ServicePoint.ConnectionLeaseTimeout = 0;
try
{
client.Send(message);
}
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
}
}
}
Edit:
It turns out the email does eventually send after 2-3 minutes. It seems as though it is being queued by the exchange server, or the SmtpClient connection eventually times out and is closed by the server.
Edit:
I have tried.
client.ServicePoint.ConnectionLeaseTimeout = 1;
client.ServicePoint.MaxIdleTime = 1;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我终于;在 StackOverflow 和其他各种研究来源的帮助下,我们找到了解决方案。通过设置 System.Net.ServicePointManager.MaxServicePointIdleTime = 1,邮件将立即发送。
这是最终的代码。
谢谢大家的帮助!特别是冰曼尼德。
I finally; after all the help from StackOverflow and other various research sources, have found the solution. By setting
System.Net.ServicePointManager.MaxServicePointIdleTime
= 1, the mail is sent immediately.Here is the final code.
Thank you all for your help! Especially icemanind.
可能的原因是连接保持打开状态。尝试在 Send 方法末尾关闭连接,看看是否有效。
编辑:现在 SmtpClient 实现了 IDispose,这似乎是 .NET 4.0 中的情况。
A possible cause is that the connection is being kept open. Try closing the connection at the end of the Send method and see if that works.
Edit: This appears to be the case in .NET 4.0 now that the SmtpClient implements IDispose.
我打赌您已经安装了诺顿防病毒软件。这似乎是诺顿防病毒软件的一个已知问题。您可以通过打开诺顿防病毒软件并禁用电子邮件工具来解决此问题。让我知道这是否适合您。
I am betting you have Norton Antivirus installed. This seems to be a known issue with Norton Antivirus. You can fix this by opening Norton antivirus and disabling the email tools. Let me know if that works for you.
好的测试人员...如果您想解决诺顿问题,这非常简单。添加以下行:
这将导致 smtp 客户端加密您的连接,从而将其发送到诺顿监控的不同端口上。看看是否有效!
ok Tester...If you want to get around the Norton Issue, its pretty simple. Add the following line:
This will cause the smtp client to encrypt your connection, thus sending it on a different port then what norton monitors. See if that works!
另外,对于 System.Net.Mail.MailMessage 等任何一次性类型,您应该使用“using”块:
Also, for any disposable types like System.Net.Mail.MailMessage you should used a "using" block:
System.Net.Mail.MailMessage
和System.Net.Mail.SmtpClient
都实现了IDisposable
,这意味着您需要调用完成后将其丢弃
。 IE:System.Net.Mail.MailMessage
andSystem.Net.Mail.SmtpClient
both implementIDisposable
, which means that you need to callDispose
on them after you're done. IE: