System.Net.Mail 创建无效的电子邮件和 eml 文件?在主机名中插入额外的点

发布于 2024-11-19 04:31:03 字数 2036 浏览 2 评论 0原文

.NET 的 SmtpClient 似乎正在创建在主机名中带有额外点的电子邮件(如果该点出现在 MIME 编码行的开头)(例如 test.com 有时显示为 test..com)。示例代码:

[TestMethod]
public void TestEmailIssue()
{
    var mail = new System.Net.Mail.MailMessage();
    var smtpClient = new System.Net.Mail.SmtpClient();

    mail.To.Add("[email protected]");
    mail.Subject = "Test";
    mail.From = new System.Net.Mail.MailAddress("[email protected]");
    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:[电子邮件受保护]

X-Receiver:[电子邮件受保护]

MIME 版本:1.0

发件人:[电子邮件受保护]

收件人:[电子邮件受保护]

日期:2011年7月6日15:55:28 -0400

主题:测试

内容类型:文本/纯文本;字符集=us-ascii

内容传输编码:引用可打印

您好,这是对该问题的简短测试:https://test=

..com/'>https://test.com/:=20

发送文件或在 Outlook(或任何其他程序)中打开时,会显示双点(即 test..com) 。请注意,如果我删除多余的空格(在“is a”中),则 test.com 将正确显示,因为点不再出现在行的开头。

这在尝试发送网站地址时会导致问题,我们接到客户的电话,说他们无法点击我们的链接。

还有其他人经历过吗?除了编写自己的编码之外,我们如何解决这个问题?

It appears that .NET's SmtpClient is creating emails with an extra dot in host names if the dot was to appear at the beginning of a MIME encoded line (e.g. test.com sometimes shows up as test..com). Example code:

[TestMethod]
public void TestEmailIssue()
{
    var mail = new System.Net.Mail.MailMessage();
    var smtpClient = new System.Net.Mail.SmtpClient();

    mail.To.Add("[email protected]");
    mail.Subject = "Test";
    mail.From = new System.Net.Mail.MailAddress("[email protected]");
    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);
}

This creates an .eml file that looks like this:

X-Sender: [email protected]

X-Receiver: [email protected]

MIME-Version: 1.0

From: [email protected]

To: [email protected]

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

When sending the file, or opening in Outlook (or any other program), the double dots show up (i.e. test..com). Note that if I remove the extra space (in "is a"), that test.com shows correctly since the dot no longer appears at the beginning of the line.

This causes a problem when trying to send website addresses, and we get calls from clients saying this they cannot click our links.

Has anyone else experienced this? How can we resolve this issue other than writing our own encoding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花细雨 2024-11-26 04:31:03

这实际上符合 RFC 2821(4.5.2 透明度)

在发送一行邮件文本之前,SMTP 客户端会检查该行的第一个字符。如果是句点,则在行首插入一个附加句点。

.Net 只是以“准备传输”模式存储文件,这意味着它不必在发送之前对电子邮件进行修改,而是可以按原样传输。不幸的是,这种格式显然与 Outlook Express 的 EML 格式并不 100% 相同。您应该能够将编码设置为 UTF-8(或类似的编码),这将为您启动 Base-64 编码。

mail.BodyEncoding = System.Text.Encoding.UTF8;

This is actually per RFC 2821 (4.5.2 Transparency)

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 is just storing the file in "ready to transmit" mode which means that it doesn't have to monkey with the email before sending, instead it can transmit it as is. Unfortunately this format isn't 100% the same as Outlook Express's EML format apparently. You should be able to set the encoding to UTF-8 (or something similar) and that will kick in Base-64 encoding for you.

mail.BodyEncoding = System.Text.Encoding.UTF8;
多情癖 2024-11-26 04:31:03

在 .Net 2.0 中,

X-Sender: [email protected]
X-Receiver: [email protected]
MIME-Version: 1.0
From: [email protected]
To: [email protected]
Date: 6 Jul 2011 21:29:04 +0100
Subject: Test
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

Hello this is  a short test of the issue: <a href=3D'https://test.com/'>https://test.com/</a>:=

它看起来像是以每行特定的字符长度换行文本。我依稀记得 .Net 2.0 中有一个问题,默认情况下它不会执行此操作,这可能会导致垃圾邮件过滤器出现问题。

事实上,增加消息的大小会在 .Net 4.0 中产生以下结果:

X-Sender: [email protected]
X-Receiver: [email protected]
MIME-Version: 1.0
From: [email protected]
To: [email protected]
Date: 6 Jul 2011 21:34:21 +0100
Subject: Test
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

Hello this is  a short test of the sssssssssssssssssissue: <a hre=
f=3D'https://test.com/'>https://test.com/</a>:=20

看起来像一个错误。

解决方法可能是将 BodyEncoding 更改为 ASCII 以外的其他内容。

In .Net 2.0

X-Sender: [email protected]
X-Receiver: [email protected]
MIME-Version: 1.0
From: [email protected]
To: [email protected]
Date: 6 Jul 2011 21:29:04 +0100
Subject: Test
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

Hello this is  a short test of the issue: <a href=3D'https://test.com/'>https://test.com/</a>:=

It looks like it is wrapping the text at a certain character length per line. I vaguely remember there was an issue in .Net 2.0 where by default it doesn't do this which can cause problems with spam filters.

In fact increasing the size of the message gives the following in .Net 4.0:

X-Sender: [email protected]
X-Receiver: [email protected]
MIME-Version: 1.0
From: [email protected]
To: [email protected]
Date: 6 Jul 2011 21:34:21 +0100
Subject: Test
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

Hello this is  a short test of the sssssssssssssssssissue: <a hre=
f=3D'https://test.com/'>https://test.com/</a>:=20

Seems like a bug.

A workaround might be to change the BodyEncoding to something other than ASCII.

还给你自由 2024-11-26 04:31:03

查看.NET 4源代码,也许您遇到的情况与MailWriter.WriteAndFold方法有关。 MailWriter 类中还有一个

static int writerDefaultLineLength = 76

变量,表示每行的字符数。也许是因为您删除了一个额外的空格字符,它才开始工作。

Looking at .NET 4 source code, maybe what you are experiencing has something to do with MailWriter.WriteAndFold method. Also in MailWriter class there is

static int writerDefaultLineLength = 76

variable, meaning character count per line. Maybe because you removed one extra space character, it started working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文