c# - 无法在 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)/(没用)
编辑:
问题是因为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) { ... }
推荐阅读
- c - 存储在未使用的内存空间中的值是什么?
- sql - 尝试在 SQL 中更新表
- r - 为什么在 R 中制作简单的条形图如此困难?如何轻松地将数据框变成条形图?
- r - RMarkdown:如何在单个块上使用钩子?
- python - 页面加载但不显示从数据库请求的内容
- javascript - 循环遍历一个对象数组,为每个对象调用一个promise并在它完成时记录
- javascript - 如何使用 react dropzone 预览 pdf
- excel - 自定义格式单元格以使用 VBA 代码添加句子
- python - 如何从不同目录中的不同文件中获取变量到 python 文件中
- recursion - 递归移动JavaFX文本标签的坐标