gpt4 book ai didi

c# - 处置派生类

转载 作者:行者123 更新时间:2023-11-30 19:52:23 25 4
gpt4 key购买 nike

我有一个派生自 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。不。微软本身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);
}

关于c# - 处置派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58338818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com