使用下面的 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)
我嘗試過來自互聯網的不同解決方案,但都沒有奏效,請任何人幫助解決問題
嘗試的解決方案:
無法從 MimeMessage 決議 tnef 部分
https://www.csharpcodi.com/csharp-examples/MimeKit.InternetAddress.TryParseLocalPart ( byte[], ref int, int, bool, out string)/(沒用)
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));克服錯誤
uj5u.com熱心網友回復:
我認為您的 MailboxAddress ctor 引數順序錯誤。
https://github.com/jstedfast/MimeKit/blob/master/MimeKit/MailboxAddress.cs#L163
public MailboxAddress (string name, string address) { ... }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/330268.html
標籤:C# 电子邮件 .net-4.5 模仿工具包 哑剧消息
