在申请结束前不会发送电子邮件

时间:2010-05-25 15:55:28

标签: c# email smtpclient

我有一个使用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;

6 个答案:

答案 0 :(得分:5)

我终于;在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.
            }
        }
    }
}

谢谢大家的帮助!特别是冰人。

答案 1 :(得分:3)

可能的原因是连接保持打开状态。尝试在Send方法的末尾关闭连接,看看是否有效。

编辑:现在,在.NET 4.0中,SmtpClient实现了IDispose。

答案 2 :(得分:2)

我打赌你安装了Norton Antivirus。这似乎是Norton Antivirus的一个已知问题。您可以通过打开Norton防病毒并禁用电子邮件工具来解决此问题。如果这对您有用,请告诉我。

答案 3 :(得分:1)

ok Tester ...如果你想绕过诺顿问题,它非常简单。添加以下行:

message.EnableSsl = true;

这将导致smtp客户端加密您的连接,从而将其发送到不同的端口,然后是Norton监控的端口。看看是否有效!

答案 4 :(得分:0)

此外,对于像System.Net.Mail.MailMessage这样的任何一次性类型,您应该使用“使用”块:

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; 
        try  
        { 
            client.Send(message); 
        }   
        catch(System.Exception ex)  
        { 
            System.Windows.Forms.MessageBox.Show(ex.ToString());               
        }
    } 
} 

答案 5 :(得分:0)

System.Net.Mail.MailMessageSystem.Net.Mail.SmtpClient都会实施IDisposable,这意味着您需要在完成后调用Dispose。 IE:

using (System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(this.From, this.To, this.Subject, this.Body))
{
    message.IsBodyHtml = true; 
    using(System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(this.Server))
    {
        client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
        try  
        { 
            client.Send(message); 
        }   
        catch(System.Exception ex)  
        { 
            System.Windows.Forms.MessageBox.Show(ex.ToString());               
        }
    }
}