无法在 Mimekit 中发送邮件

Cannot sent mail in Mimekit

使用以下 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)

我尝试了网上的各种解决方案,但都没有用,请任何人帮助解决问题

尝试过的解决方案:

https://www.csharpcodi.com/csharp-examples/MimeKit.InternetAddress.TryParseLocalPart(byte[],%20ref%20int,%20int,%20bool,%20out%20string)/(没用)

https://www.nopcommerce.com/en/boards/topic/90019/email-error-invalid-addr-spec-token-at-offset-0-v43(没用)

编辑: 问题是因为 message.From.Add(new MailboxAddress(FromAddress, "Notification")); 它的顺序错误所以改为 message.From.Add(new MailboxAddress("Notification",FromAddress)); 来克服错误

我认为你的 MailboxAddress ctor 参数顺序错误。

https://github.com/jstedfast/MimeKit/blob/master/MimeKit/MailboxAddress.cs#L163

public MailboxAddress (string name, string address) { ... }