У меня есть приложение, которое использует 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());
}
}
}
}
Edit:
Оказывается, электронное письмо в конце концов отправляется через 2-3 минуты. Похоже, что он находится в очереди на сервере Exchange, или соединение SmtpClient в конечном итоге истекает и закрывается сервером.
Edit:
Я пытался.
client.ServicePoint.ConnectionLeaseTimeout = 1;
client.ServicePoint.MaxIdleTime = 1;