使用以下代码从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)
我已经尝试了不同的解决方案从互联网,但都没有工作,谁请帮助解决问题
尝试解决方案:
无法从MimeMessage解析tnef部分
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 (No Use)
编辑:问题是由于message.From.Add(new MailboxAddress(FromAddress, "Notification"));
的顺序错误,因此更改为message.From.Add(new MailboxAddress("Notification",FromAddress));
以克服错误
我想你只是把MailboxAddress函数参数的顺序搞错了。
https://github.com/jstedfast/MimeKit/blob/master/MimeKit/MailboxAddress.cs L163
public MailboxAddress (string name, string address) { ... }