2

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

6 回答 6

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.
            }
        }
    }
}

感谢大家的帮助!尤其是冰人。

于 2010-05-26T17:15:30.887 回答
3

一个可能的原因是连接保持打开状态。尝试在 Send 方法结束时关闭连接,看看是否可行。

编辑:现在 .NET 4.0 中似乎就是这种情况,因为 SmtpClient 实现了 IDispose。

于 2010-05-25T15:58:33.697 回答
2

我打赌你已经安装了诺顿防病毒软件。这似乎是 Norton Antivirus 的一个已知问题。您可以通过打开 Norton 防病毒软件并禁用电子邮件工具来解决此问题。让我知道这是否适合你。

于 2010-05-25T15:59:25.577 回答
1

好的测试器...如果您想解决诺顿问题,它非常简单。添加以下行:

message.EnableSsl = true;

这将导致 smtp 客户端加密您的连接,从而将其发送到与诺顿监控不同的端口。看看有没有用!

于 2010-05-25T16:26:19.147 回答
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());               
        }
    } 
} 
于 2010-05-25T16:01:29.923 回答
0

System.Net.Mail.MailMessage并且System.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());               
        }
    }
} 
于 2010-05-25T16:04:21.890 回答