C# 邮件地址:在邮件标题中发现无效字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10334804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:26:52  来源:igfitidea点击:

MailAddress: An invalid character was found in the mail header

c#.netemailexception-handlingmailaddress

提问by Boguslaw Buczek

I am using MailAddress to create a fax email. We are using system called SatisFAXtion v 8.6. To provide a cover page for faxing I can add a template location to the mail.TO address.

我正在使用 MailAddress 创建传真电子邮件。我们正在使用名为 SatisFAXtion v 8.6 的系统。为了提供传真封面,我可以在 mail.TO 地址中添加一个模板位置。

The format looks like this:

格式如下所示:

12125551234^template=cp\[email protected]

When compiling it works fine but I need two backslashes so I added two more as escape characters.

编译时它工作正常,但我需要两个反斜杠,所以我又添加了两个作为转义字符。

When I run the program I get an exception:

当我运行程序时,出现异常:

Message: An invalid character was found in the mail header:
StackTrace: at System.Net.Mail.MailAddressParser.ParseLocalPart(String data, Int32& index, Boolean expectAngleBracket, Boolean expectMultipleAddresses)
   at System.Net.Mail.MailAddressParser.ParseAddress(String data, Boolean expectMultipleAddresses, Int32& index)
   at System.Net.Mail.MailAddressParser.ParseMultipleAddresses(String data)
   at System.Net.Mail.MailAddressCollection.ParseValue(String addresses)
   at System.Net.Mail.MailAddressCollection.Add(String addresses)
   at System.Net.Mail.Message..ctor(String from, String to)
   at System.Net.Mail.MailMessage..ctor(String from, String to)

Is there a way to insert backslashes in an email address in MailAddress object?

有没有办法在 MailAddress 对象的电子邮件地址中插入反斜杠?

采纳答案by RB.

From Wikipedia:

来自维基百科

The restrictions for special characters are that they must only be used when contained between quotation marks, and that 3 of them (The space, backslash \ and quotation mark " (ASCII: 32, 92, 34)) must also be preceded by a backslash \ (e.g. "\ \\"").

对特殊字符的限制是它们只能在包含在引号之间时使用,并且其中的 3 个(空格、反斜杠 \ 和引号 " (ASCII: 32, 92, 34))前面也必须有反斜杠\(例如“\ \\””)。

Therefore, I don't think your email address is valid as the backslashes are not contained between quotation marks.

因此,我认为您的电子邮件地址无效,因为引号之间不包含反斜杠。

If you could change the address to something like 12125551234^template="cp\\"[email protected]I think it would be ok.

如果您可以将地址更改为类似12125551234^template="cp\\"[email protected]我认为的内容就可以了。

回答by Wiktor Zychla

Years ago I found a workaround over a similar issue. The issue was in the DisplayName parameter in the constructor of MailAddress.

几年前,我找到了解决类似问题的解决方法。问题出在 MailAddress 构造函数中的 DisplayName 参数中。

Maybe it will help you too:

也许它也会帮助你:

http://netpl.blogspot.com/2008/09/smtpclient-exception-invalid-character.html

http://netpl.blogspot.com/2008/09/smtpclient-exception-invalid-character.html

回答by HydPhani

I received this error when if this is modified to "network" --- then error got resolved. ( My understanding is - Incase of specified pickupdirectory option, the header -encoding utf-8 (base64) was giving error )

如果将其修改为“网络”,我会收到此错误 --- 然后错误得到解决。(我的理解是 - 在指定的pickupdirectory 选项的情况下,标头-encoding utf-8 (base64) 给出了错误)

Hope it helps

希望能帮助到你