【发布时间】:2021-12-05 12:32:43
【问题描述】:
使用以下 MimeKit 代码从 c# 发送邮件时
var message = new MimeMessage();
message.From.Add(new MailboxAddress(FromAddress, "Notification"));
foreach (var address in Toaddress.Split(','))
{
message.To.Add(new MailboxAddress(address.Trim(), ""));
}
message.Subject = Subject;
message.Body = new TextPart("plain") { Text = "Test Message" };
using (var client = new SmtpClient())
{
client.Connect(EmailHostName, Portnumber, SecureSocketOptions.StartTls);
client.Authenticate(UserName, Password);
client.Send(message);
client.Disconnect(true);
}
我得到以下异常
MimeKit.ParseException: Invalid addr-spec token at offset 0
at MimeKit.InternetAddress.TryParseAddrspec(Byte[] text, Int32& index, Int32 endIndex, Byte[] sentinels, Boolean throwOnError, String& addrspec, Int32& at)
at MimeKit.MailboxAddress.set_Address(String value)
at MimeKit.MailboxAddress..ctor(Encoding encoding, String name, String address)
at MimeKit.MailboxAddress..ctor(String name, String address)
我尝试了网上不同的解决方案,但都没有成功,请大家帮忙解决问题
尝试过的解决方案:
Unable to parse tnef part from MimeMessage
编辑:
问题是因为message.From.Add(new MailboxAddress(FromAddress, "Notification"));它的顺序错误所以改为message.From.Add(new MailboxAddress("Notification",FromAddress));来解决错误
【问题讨论】:
-
您能否提供更多有关您尝试过什么的详细信息?运行此代码时您提供了哪些地址(一些示例)?
-
您是否调试过
FromAddress和Toaddress,检查它们是否/包含有效的电子邮件地址?
标签: c# email .net-4.5 mimekit mime-message