为 .NetCore MailKit smtp 实用程序快速添加电子邮件地址
quickly adding email address for .NetCore MailKit smtp utility
我找到了一个用于 MailKit .NetCore 的小电子邮件实用程序。
我成功了,但我不知道如何添加多个电子邮件收件人。
我可以这样:
EmailAddress toAddress = new EmailAddress()
{
Name = "Ken Watanabe",
Address = "kw100@xxxx.com"
};
var newMessage = new EmailMessage()
然后像这样添加到新的 EmailMessage 中:
newMessage.ToAddresses.Add(toAddress);
EmailMessage class 看起来像这样:
public class EmailMessage
{
public EmailMessage()
{
ToAddresses = new List<EmailAddress>();
FromAddresses = new List<EmailAddress>();
}
public List<EmailAddress> ToAddresses { get; set; }
public List<EmailAddress> FromAddresses { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
}
然后传递给另一个方法发送:
var message = new MimeMessage();
message.To.AddRange(emailMessage.ToAddresses.Select(x => new MailboxAddress(x.Name, x.Address)));
我想我可以像这样创建新的收件人:
EmailAddress toAddress1 = new EmailAddress()
{
Name = "Ken Watanabe",
Address = "kw100@xxxx.com"
};
EmailAddress toAddress2 = new EmailAddress()
{
Name = "Ryosuke Yamada",
Address = "ry1010104@yyyy.com"
};
newMessage.ToAddresses.Add(toAddress1);
newMessage.ToAddresses.Add(toAddress2);
但我想知道是否有一种不需要那么多代码的更简单的方法。
有没有办法只添加信息,而不必为每个信息创建一个单独的 EmailAddress 对象?
谢谢!
如果您可以修改 EmailAddress
class,那么最简单的更改就是制作一个 class 带有名称和电子邮件参数的构造函数,例如
public class EmailAddress
{
public EmailAddress() { }
public EmailAddress(name, address)
{
Name = name;
Address = address;
}
// ...
然后您可以创建新的电子邮件地址并将其直接添加到地址列表中。例如
newMessage.ToAddresses.Add(new EmailAddress("Ken Watanabe", "kw100@xxxx.com"));
newMessage.ToAddresses.Add(new EmailAddress("Ryosuke Yamada", "ry1010104@yyyy.com"));
我找到了一个用于 MailKit .NetCore 的小电子邮件实用程序。
我成功了,但我不知道如何添加多个电子邮件收件人。
我可以这样:
EmailAddress toAddress = new EmailAddress()
{
Name = "Ken Watanabe",
Address = "kw100@xxxx.com"
};
var newMessage = new EmailMessage()
然后像这样添加到新的 EmailMessage 中:
newMessage.ToAddresses.Add(toAddress);
EmailMessage class 看起来像这样:
public class EmailMessage
{
public EmailMessage()
{
ToAddresses = new List<EmailAddress>();
FromAddresses = new List<EmailAddress>();
}
public List<EmailAddress> ToAddresses { get; set; }
public List<EmailAddress> FromAddresses { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
}
然后传递给另一个方法发送:
var message = new MimeMessage();
message.To.AddRange(emailMessage.ToAddresses.Select(x => new MailboxAddress(x.Name, x.Address)));
我想我可以像这样创建新的收件人:
EmailAddress toAddress1 = new EmailAddress()
{
Name = "Ken Watanabe",
Address = "kw100@xxxx.com"
};
EmailAddress toAddress2 = new EmailAddress()
{
Name = "Ryosuke Yamada",
Address = "ry1010104@yyyy.com"
};
newMessage.ToAddresses.Add(toAddress1);
newMessage.ToAddresses.Add(toAddress2);
但我想知道是否有一种不需要那么多代码的更简单的方法。
有没有办法只添加信息,而不必为每个信息创建一个单独的 EmailAddress 对象?
谢谢!
如果您可以修改 EmailAddress
class,那么最简单的更改就是制作一个 class 带有名称和电子邮件参数的构造函数,例如
public class EmailAddress
{
public EmailAddress() { }
public EmailAddress(name, address)
{
Name = name;
Address = address;
}
// ...
然后您可以创建新的电子邮件地址并将其直接添加到地址列表中。例如
newMessage.ToAddresses.Add(new EmailAddress("Ken Watanabe", "kw100@xxxx.com"));
newMessage.ToAddresses.Add(new EmailAddress("Ryosuke Yamada", "ry1010104@yyyy.com"));