使用 MailboxAddress(String) 构造函数发送带有 mailKit 过时警告的电子邮件
Sending Emails with mailKit obsolete warnings with MailboxAddress(String) constructor
我有以下代码可以很好地发送电子邮件,但我收到这些警告说我应该将一些方法标记为过时。我查看了 mailKit website and it says that I should use a different constructor than MailboxAddress(String)
. I get that, but am having troubles on how to implement the way they're asking, it seems simple but i think im missing a step. This is one of the ways that's recommend MailboxAddress(String, String)
new way of doing it.
上的文档
代码如下:
控制器
public IActionResult RequestPassword()
{
var message = new Message(new string[]
{
"email@gmail.com"
},
"Test Email",
"This is the content from out email."
);
_emailSender.SendEmail(message);
return View();
}
Message.cs
为此 class 行 To.AddRange(to.Select(x => new MailboxAddress(x)));
弹出警告
public class Message
{
public List<MailboxAddress> To { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public Message(IEnumerable<string> to, string subject, string content)
{
To = new List<MailboxAddress>();
To.AddRange(to.Select(x => new MailboxAddress(x)));
Subject = subject;
Content = content;
}
}
电子邮件发件人class
为此 class 行 emailMessage.From.Add(new MailboxAddress(_emailConfig.From));
弹出警告
public class EmailSender : IEmailSender
{
private readonly EmailConfiguration _emailConfig;
public EmailSender(EmailConfiguration emailConfig)
{
_emailConfig = emailConfig;
}
// This creates the Email and then sends it.
public void SendEmail(Message message)
{
var emailMessage = CreateEmailMessage(message);
Send(emailMessage);
}
// Creates Email Message
private MimeMessage CreateEmailMessage(Message message)
{
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress(_emailConfig.From));
emailMessage.To.AddRange(message.To);
emailMessage.Subject = message.Subject;
emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Text)
{
Text = message.Content
};
return emailMessage;
}
// Sends the email
private void Send(MimeMessage mailMessage)
{
using var client = new SmtpClient();
try
{
client.Connect(_emailConfig.MailServer, _emailConfig.MailPort, false);
client.Authenticate(_emailConfig.Username, _emailConfig.Password);
client.Send(mailMessage);
}
catch (Exception)
{
throw;
}
finally
{
client.Disconnect(true);
client.Dispose();
}
}
}
界面
public interface IEmailSender
{
void SendEmail(Message message);
}
如果你想要 StartUp.cs 也请告诉我。
它只是将显示在收件人上的电子邮件地址和显示名称。
var address = new MailboxAddress("Tonton", "tonton@example.com");
输出
更新
如果要添加显示名称。你可以有这样的东西。
public class EmailAddress
{
public string Address { get; set; }
public string DisplayName { get; set; }
}
public class Message
{
public List<MailboxAddress> To { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public Message(IEnumerable<EmailAddress> to, string subject, string content)
{
To = new List<MailboxAddress>();
To.AddRange(to.Select(x => new MailboxAddress(x.DisplayName, x.Address)));
Subject = subject;
Content = content;
}
}
我有以下代码可以很好地发送电子邮件,但我收到这些警告说我应该将一些方法标记为过时。我查看了 mailKit website and it says that I should use a different constructor than MailboxAddress(String)
. I get that, but am having troubles on how to implement the way they're asking, it seems simple but i think im missing a step. This is one of the ways that's recommend MailboxAddress(String, String)
new way of doing it.
代码如下:
控制器
public IActionResult RequestPassword()
{
var message = new Message(new string[]
{
"email@gmail.com"
},
"Test Email",
"This is the content from out email."
);
_emailSender.SendEmail(message);
return View();
}
Message.cs
为此 class 行 To.AddRange(to.Select(x => new MailboxAddress(x)));
public class Message
{
public List<MailboxAddress> To { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public Message(IEnumerable<string> to, string subject, string content)
{
To = new List<MailboxAddress>();
To.AddRange(to.Select(x => new MailboxAddress(x)));
Subject = subject;
Content = content;
}
}
电子邮件发件人class
为此 class 行 emailMessage.From.Add(new MailboxAddress(_emailConfig.From));
public class EmailSender : IEmailSender
{
private readonly EmailConfiguration _emailConfig;
public EmailSender(EmailConfiguration emailConfig)
{
_emailConfig = emailConfig;
}
// This creates the Email and then sends it.
public void SendEmail(Message message)
{
var emailMessage = CreateEmailMessage(message);
Send(emailMessage);
}
// Creates Email Message
private MimeMessage CreateEmailMessage(Message message)
{
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress(_emailConfig.From));
emailMessage.To.AddRange(message.To);
emailMessage.Subject = message.Subject;
emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Text)
{
Text = message.Content
};
return emailMessage;
}
// Sends the email
private void Send(MimeMessage mailMessage)
{
using var client = new SmtpClient();
try
{
client.Connect(_emailConfig.MailServer, _emailConfig.MailPort, false);
client.Authenticate(_emailConfig.Username, _emailConfig.Password);
client.Send(mailMessage);
}
catch (Exception)
{
throw;
}
finally
{
client.Disconnect(true);
client.Dispose();
}
}
}
界面
public interface IEmailSender
{
void SendEmail(Message message);
}
如果你想要 StartUp.cs 也请告诉我。
它只是将显示在收件人上的电子邮件地址和显示名称。
var address = new MailboxAddress("Tonton", "tonton@example.com");
输出
更新
如果要添加显示名称。你可以有这样的东西。
public class EmailAddress
{
public string Address { get; set; }
public string DisplayName { get; set; }
}
public class Message
{
public List<MailboxAddress> To { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public Message(IEnumerable<EmailAddress> to, string subject, string content)
{
To = new List<MailboxAddress>();
To.AddRange(to.Select(x => new MailboxAddress(x.DisplayName, x.Address)));
Subject = subject;
Content = content;
}
}