C# 如何正确处理 SmtpClient? [英] C# how to correctly dispose of an SmtpClient?

查看:48
本文介绍了C# 如何正确处理 SmtpClient?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VS 2010 代码分析报告如下:

VS 2010 code analysis reports the following:

警告 4 CA2000:Microsoft.Reliability:在方法 'Mailer.SendMessage()' 中,对象 'client' 并未沿所有异常路径处理.在对象client"的所有引用超出范围之前,调用 System.IDisposable.Dispose.

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.

我的代码是:

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

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

我应该如何正确处理客户?

How should I correctly dispose of client?

更新:要回答 Jons 的问题,这里是处理附件功能:

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

最后更新完整课程列表(简短)

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

这样,即使在 Send 方法调用期间抛出异常,客户端也会被处理.您应该很少需要显式调用 Dispose - 它应该几乎总是在 using 语句中.

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.

但是,这里并不清楚附件是如何涉及的.你的类是否实现了 IDisposable 本身?如果是这样,那可能是处理可能是成员变量的附件的地方.如果您需要绝对确保它们在这里得到处理,您可能需要:

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

这篇关于C# 如何正确处理 SmtpClient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆