gpt4 book ai didi

c# - 直到应用程序关闭才发送电子邮件

转载 作者:行者123 更新时间:2023-11-30 19:13:44 25 4
gpt4 key购买 nike

我有一个使用 SmtpClient 发送电子邮件的应用程序,但是在应用程序关闭之前不会发送电子邮件。我已经搜索和搜索以找到解决问题的方法,但我找不到。

系统确实安装了 Symantec 防病毒软件,这可能是问题所在。

有人能解决这个问题吗?

这是我正在使用的代码。

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;

最佳答案

我终于;在 StackOverflow 和其他各种研究资源的帮助下,找到了解决方案。通过设置 System.Net.ServicePointManager.MaxServicePointIdleTime = 1,立即发送邮件。

这是最终代码。

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;

int temp = System.Net.ServicePointManager.MaxServicePointIdleTime; //<- Store the original value.
System.Net.ServicePointManager.MaxServicePointIdleTime = 1; //<- Change the idle time to 1.

try
{
client.Send(message);
}
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
finally
{
System.Net.ServicePointManager.MaxServicePointIdleTime = temp; //<- Set the idle time back to what it was.
}
}
}
}

谢谢大家的帮助!特别是 icemanind。

关于c# - 直到应用程序关闭才发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2906281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com