处置派生 Class

Dispose Derived Class

我有一个派生自 System.Net.Mail.MailMessage 的 class。 class 将只包含一些 HTML 格式的电子邮件正文静态文本。

public sealed class CustomMessage : MailMessage
{
    public void SendTo(params string[] addresses)
    {
        foreach (var address in addresses)
        {
            SendTo(address);
        }
    }

    public void SendTo(string address)
    {
        To.Add(address);
    }

    // ...
}

然后我可以将其包装在 using 语句中:

using (var message = new CustomMessage())
{
     message.SendTo("address1", "address2");
     //...
}

MailMessage 基础 class 实现 IDisposable:

protected virtual void Dispose(bool disposing)
{
  if (!disposing || this.disposed)
    return;
  this.disposed = true;
  if (this.views != null)
    this.views.Dispose();
  if (this.attachments != null)
    this.attachments.Dispose();
  if (this.bodyView == null)
    return;
  this.bodyView.Dispose();
}

尽管我的 class 中没有真正需要处理的东西,但我是否仍需要重写 Dispose(bool) 方法?

bool disposed = false;

protected override void Dispose(bool disposing)
{
    if (disposed)
     return; 

     if (disposing) 
     {
        // Nothing managed to dispose.
     }

     // Nothing unmanaged to dispose.

     disposed = true;
     base.Dispose(disposing);
}

不,CustomMessage 继承了基本的 Dispose()Dispose(bool) 方法。它不需要覆盖它们,除非它必须自己做一些额外的处理。

我刚刚注意到你使用 System.Net.Mail.MailMessage。不。它已经过时,Microsoft 本身也强烈警告不要使用它。

替代自定义消息

在任何情况下,最好编写一个扩展方法来添加多个收件人,而不是创建一条新消息 class。您可以在 MimeKit 中创建类似的东西,建议替换 SmtpClient :

static public void AddRecipients(this MimeMessage message,IEnumerable<string> addresses)
{
    var ads=addresses.Select(ad=>MailboxAddress.Parse(ad));
    message.To.AddRange(ads);
}

或者 SmptClient 的这个

static public void AddRecipients(this MailMessage message,IEnumerable<string> addresses)
{
    foreach (var address in addresses)
    {
        message.To.Add(address);
    }
}

SmptClient 已过时

我刚刚注意到你使用 System.Net.Mail.MailMessage。不。 Microsoft 本身 warns against using SmptClient 非常 强格式警告中,在 SmptClient 文档页面的最顶部:

Warning

This API is now obsolete.

事实上,您应该已经收到编译器警告了:

SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead

您应该更改代码以使用 MailKit。您可能不必首先创建自定义消息。

对于简单的情况,API 类似于 SmptClient,甚至还有一个从 System.Net.Mail.MailMessage 到 MimeKit 自己的 MimeMessage 的强制转换操作,使转换更容易。

MailKit's Github landing page 中的示例显示了它的易用性

var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey Tribbiani", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Mrs. Chanandler Bong", "chandler@friends.com"));
message.Subject = "How you doin'?";

message.Body = new TextPart ("plain") {
    Text = @"Hey Chandler,

I just wanted to let you know that Monica and I were going to go play some paintball, you in?

-- Joey"
};

using (var client = new SmtpClient ()) {
    // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
    client.ServerCertificateValidationCallback = (s,c,h,e) => true;

    client.Connect ("smtp.friends.com", 587, false);

    // Note: only needed if the SMTP server requires authentication
    client.Authenticate ("joey", "password");

    client.Send (message);
    client.Disconnect (true);
}

因为你没有任何额外的东西要处理,所以你不需要重写 Dispose。