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 分後に送信されることがわかりました。Exchange サーバーによってキューに入れられているように見えます。または、SmtpClient 接続が最終的にタイムアウトになり、サーバーによって閉じられたようです。

編集:

私が試してみました。

client.ServicePoint.ConnectionLeaseTimeout = 1;
client.ServicePoint.MaxIdleTime = 1;
4

6 に答える 6

5

ついに; StackOverflow やその他のさまざまな研究ソースからのすべての助けを借りて、解決策を見つけました。= 1 に設定するSystem.Net.ServicePointManager.MaxServicePointIdleTimeと、メールはすぐに送信されます。

これが最終的なコードです。

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 メソッドの最後で接続を閉じてみて、それが機能するかどうかを確認してください。

編集:これは、SmtpClient が IDispose を実装するようになった .NET 4.0 の場合のようです。

于 2010-05-25T15:58:33.697 に答える
2

Norton Antivirus がインストールされていると思います。これは Norton Antivirus の既知の問題のようです。これを修正するには、ノートン アンチウイルスを開いて電子メール ツールを無効にします。それがうまくいくかどうか教えてください。

于 2010-05-25T15:59:25.577 に答える
1

わかりましたテスター...ノートンの問題を回避したい場合は、非常に簡単です。次の行を追加します。

message.EnableSsl = true;

これにより、smtp クライアントが接続を暗号化するため、ノートンが監視するポートとは別のポートで送信します。それが機能するかどうかを確認してください!

于 2010-05-25T16:26:19.147 に答える
0

また、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());               
        }
    } 
} 
于 2010-05-25T16:01:29.923 に答える
0

System.Net.Mail.MailMessage両方ともSystem.Net.Mail.SmtpClientimplementationIDisposableです。つまり、Dispose完了後にそれらを呼び出す必要があります。いいえ:

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 に答える