我有一个从System.Net.Mail.MailMessage派生的类。该类将仅包含电子邮件正文的一些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基类实现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();
}


尽管我的课中没有真正要处理的东西,但是我仍然需要重写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本身强烈警告不要使用它。

自定义消息的替代

无论如何,写一个扩展方法来添加多个收件人比创建一个新的消息类更好。您可以在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。别。在SmptClient的文档页面的顶部,Microsoft本身以非常强烈的格式警告warns against using SmptClient


  警告
  
  该API现在已过时。


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


  SmtpClient及其类型的网络设计不良,我们强烈建议您改用https://github.com/jstedfast/MailKithttps://github.com/jstedfast/MimeKit


您应该更改代码以使用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", "[email protected]"));
message.To.Add (new MailboxAddress ("Mrs. Chanandler Bong", "[email protected]"));
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);
}

10-07 15:45