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;
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.
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.
}
}
}
}
Thank you all for your help! Especially icemanind.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With