如果点出现在 MIME 编码行的开头(例如 test.com 有时显示为 test..com),.NET 的 SmtpClient 似乎正在创建主机名中带有额外点的电子邮件。示例代码:
[TestMethod]
public void TestEmailIssue()
{
var mail = new System.Net.Mail.MailMessage();
var smtpClient = new System.Net.Mail.SmtpClient();
mail.To.Add("Test@test.com");
mail.Subject = "Test";
mail.From = new System.Net.Mail.MailAddress("test@test.com");
mail.Body = "Hello this is a short test of the issue:"
+" <a href='https://test.com/'>https://test.com/</a>: ";
smtpClient.PickupDirectoryLocation = "C:\\temp\\";
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
smtpClient.Send(mail);
}
这将创建一个如下所示的 .eml 文件:
X-Sender: test@test.com
X-Receiver: Test@test.com
MIME-Version: 1.0
From: test@test.com
To: Test@test.com
Date: 6 Jul 2011 15:55:28 -0400
Subject: Test
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
Hello this is a short test of the issue: https://test=
..com/'>https://test.com/:=20
发送文件或在 Outlook(或任何其他程序)中打开文件时,会出现双点(即 test..com)。请注意,如果我删除多余的空格(在“is a”中),test.com 会正确显示,因为点不再出现在行的开头。
这会在尝试发送网站地址时造成问题,我们接到客户的电话说他们无法点击我们的链接。
有没有人遇到过这种情况?除了编写自己的编码之外,我们如何解决这个问题?
最佳答案
这实际上是根据 RFC 2821 (4.5.2 透明度)
Before sending a line of mail text, the SMTP client checks the first character of the line. If it is a period, one additional period is inserted at the beginning of the line.
.Net 只是将文件存储在“准备传输”模式,这意味着它不必在发送前处理电子邮件,而是可以按原样传输。不幸的是,这种格式显然不是 100% 与 Outlook Express 的 EML 格式相同。您应该能够将编码设置为 UTF-8(或类似的东西),这将为您启动 Base-64 编码。
mail.BodyEncoding = System.Text.Encoding.UTF8;
关于c# - System.Net.Mail 创建无效的电子邮件和 eml 文件?在主机名中插入额外的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6602318/