申请关闭之前不会发送电子邮件

发布于 2024-09-03 02:21:46 字数 1996 浏览 5 评论 0原文

我有一个使用 SmtpClient 发送电子邮件的应用程序,但直到应用程序关闭后才会发送电子邮件。我一直在寻找解决问题的方法,但我找不到。

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

有人有解决这个问题的办法吗?

这是我正在使用的代码。

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不一样的天空 2024-09-10 02:21:46

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

谢谢大家的帮助!特别是冰曼尼德。

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.

梦一生花开无言 2024-09-10 02:21:46

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

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

A possible cause is that the connection is being kept open. Try closing the connection at the end of the Send method and see if that works.

Edit: This appears to be the case in .NET 4.0 now that the SmtpClient implements IDispose.

薯片软お妹 2024-09-10 02:21:46

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

I am betting you have Norton Antivirus installed. This seems to be a known issue with Norton Antivirus. You can fix this by opening Norton antivirus and disabling the email tools. Let me know if that works for you.

老街孤人 2024-09-10 02:21:46

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

message.EnableSsl = true;

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

ok Tester...If you want to get around the Norton Issue, its pretty simple. Add the following line:

message.EnableSsl = true;

This will cause the smtp client to encrypt your connection, thus sending it on a different port then what norton monitors. See if that works!

○愚か者の日 2024-09-10 02:21:46

另外,对于 System.Net.Mail.MailMessage 等任何一次性类型,您应该使用“using”块:

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());               
        }
    } 
} 

Also, for any disposable types like System.Net.Mail.MailMessage you should used a "using" block:

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());               
        }
    } 
} 
℡寂寞咖啡 2024-09-10 02:21:46

System.Net.Mail.MailMessageSystem.Net.Mail.SmtpClient 都实现了 IDisposable,这意味着您需要调用 完成后将其丢弃。 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());               
        }
    }
} 

System.Net.Mail.MailMessage and System.Net.Mail.SmtpClient both implement IDisposable, which means that you need to call Dispose on them after you're done. 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());               
        }
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文