C#中如何正确处置的SmtpClient的?如何正确、SmtpClient

2023-09-07 11:34:32 作者:师太别出声是老衲

VS 2010 code分析报告如下:

警告4 CA2000:Microsoft.Reliability:在方法Mailer.SendMessage()',对象'客户'是不是沿着所有的异常路径。所有引用之前调用System.IDisposable.Dispose对象'客户'它超出范围。

我的code是:

 公共无效的SendMessage()
    {
        SmtpClient客户端=新SmtpClient();

        client.Send(消息);
        client.Dispose();
        DisposeAttachments();
    }
 

应该如何正确处理客户?

更新:回答JONS的问题,这里是处置附件功能:

 私人无效DisposeAttachments()
{
    的foreach(附件附件Message.Attachments)
    {
        attachment.Dispose();
    }
    Message.Attachments.Dispose();
    消息= NULL;
}
 

最新更新满级的上市(其短)

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Net.Mail;

公共类梅勒
    {
    公共MailMessage消息
    {
        得到;
        组;
    }

    公共梅勒(MailMessage消息)
    {
        this.Message =消息;
    }

    公共无效的SendMessage()
    {
        使用(SmtpClient客户端=新SmtpClient())
        {
            client.Send(消息);
        }
        DisposeAttachments();
    }

    私人无效DisposeAttachments()
    {
        的foreach(附件附件Message.Attachments)
        {
            attachment.Dispose();
        }
        Message.Attachments.Dispose();
        消息= NULL;
    }
    }
 
手机即使有100个APP还是不满足 你可能需要这5个超实用的基础软件

解决方案

 公共无效的SendMessage()
{
    使用(SmtpClient客户端=新SmtpClient())
    {
        client.Send(消息);
    }
    DisposeAttachments();
}
 

这样,如果一个例外是发送方法调用期间抛出的客户端将被连处理。你应该很少需要处置显式调用 - 这几乎是可以在使用语句

不过,目前还不清楚该附件是如何在这里参与。请问您的类实现的IDisposable 本身?如果是这样,这可能是处置这是presumably成员变量附件的地方。如果你需要绝对确保他们得到处置就在这里,你可能需要:

 公共无效的SendMessage()
{
    尝试
    {
        使用(SmtpClient客户端=新SmtpClient())
        {
            client.Send(消息);
        }
    }
    最后
    {
        DisposeAttachments();
    }
}
 

VS 2010 code analysis reports the following:

Warning 4 CA2000 : Microsoft.Reliability : In method 'Mailer.SendMessage()', object 'client' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'client' before all references to it are out of scope.

My code is :

public void SendMessage()
    {
        SmtpClient client = new SmtpClient();

        client.Send(Message);
        client.Dispose(); 
        DisposeAttachments(); 
    }

How should I correctly dispose of client?

Update: to answer Jons question, here is the dispose attachments functionality:

private void DisposeAttachments()
{
    foreach (Attachment attachment in Message.Attachments)
    {
        attachment.Dispose();
    }
    Message.Attachments.Dispose();
    Message = null; 
}

Last Update full class listing (its short)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

public class Mailer
    {
    public MailMessage Message
    {
        get;
        set;
    }

    public Mailer(MailMessage message)
    {
        this.Message = message; 
    }

    public void SendMessage()
    {
        using (SmtpClient client = new SmtpClient())
        {
            client.Send(Message);
        }
        DisposeAttachments(); 
    }

    private void DisposeAttachments()
    {
        foreach (Attachment attachment in Message.Attachments)
        {
            attachment.Dispose();
        }
        Message.Attachments.Dispose();
        Message = null; 
    }
    }

解决方案

public void SendMessage()
{
    using (SmtpClient client = new SmtpClient())
    {
        client.Send(Message);
    }
    DisposeAttachments(); 
}

That way the client will be disposed even if an exception is thrown during the Send method call. You should very rarely need to call Dispose explicitly - it should almost always be in a using statement.

However, it's not clear how the attachments are involved here. Does your class implement IDisposable itself? If so, that's probably the place to dispose of the attachments which are presumably member variables. If you need to make absolutely sure they get disposed right here, you probably need:

public void SendMessage()
{
    try
    {
        using (SmtpClient client = new SmtpClient())
        {
            client.Send(Message);
        }
    }
    finally
    {
        DisposeAttachments(); 
    }
}