简体   繁体   English

我可以在 System.Net.Mail 中关闭 email 地址验证吗?

[英]Can I turn off the email address validation in System.Net.Mail?

I'm trying to talk to Fax server software using an email.我正在尝试使用 email 与传真服务器软件交谈。 The fax server will accept formatted SMTP mails and covert them to faxes and send them to the fax number defined in the to address.传真服务器将接受格式化的 SMTP 邮件并将它们转换为传真并将它们发送到收件人地址中定义的传真号码。 This has been manually tested by sending an email from Outlook via the same server.这已通过同一服务器从 Outlook 发送 email 手动测试。

Here's my problem - System.Net.Mail is throwing an System.FormatException: The specified string is not in the form required for an e-mail address.这是我的问题 - System.Net.Mail 抛出System.FormatException: The specified string is not in the form required for an e-mail address. exception due to the format of the email address that I am trying to send to由于我尝试发送到的 email 地址的格式导致的异常

Is there any way I can turn off/change this validation because the email address may not be RFC compliant but it will work if the email gets sent有什么方法可以关闭/更改此验证,因为 email 地址可能不符合 RFC,但如果 email 被发送,它将起作用

ie I want to send to [RFax:User@/FN=0123456789] including the square brackets即我想发送到 [RFax:User@/FN=0123456789] 包括方括号

You can send to this as an e-mail address in Outlook您可以在 Outlook 中作为电子邮件地址发送到此

Cheers Chris干杯克里斯

EDIT编辑

This is a cut-down version of the class I'm using to bypass the validation.这是我用来绕过验证的 class 的精简版本。 There are two ways of doing it - one by overriding the constructor and setting the internal attribute directly, the other using an internal constructor.有两种方法 - 一种通过覆盖构造函数并直接设置内部属性,另一种使用内部构造函数。 They have slightly different effects if there are spaces in the email address如果 email 地址中有空格,它们的效果会略有不同

using System;
using System.Reflection;

namespace Mail
{
    public class UnverifiedEmailAddress : System.Net.Mail.MailAddress
    {
        /// <summary>
    /// Constructor to bypass the validation of MailAddress
    /// </summary>
    /// <param name="address">Email address to create</param>
    public UnverifiedEmailAddress(string address)
        : base("a@a")
    {
        FieldInfo field = typeof(System.Net.Mail.MailAddress).GetField("address", BindingFlags.Instance | BindingFlags.NonPublic);
        field.SetValue(this, address);
    }

    /// <summary>
    /// Static method to create an unverifed email address bypassing the address validation
    /// </summary>
    /// <param name="address">Email address to create</param>
    /// <param name="displayName">Display name for email address</param>
    /// <returns></returns>
    private static System.Net.Mail.MailAddress GetUnverifiedEmailAddress(string address, string displayName)
    {
            ConstructorInfo cons = typeof(System.Net.Mail.MailAddress).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
                                                                null,
                                                                new Type[] { typeof(string), typeof(string), typeof(UInt32) },
                                                                null);

            object obj = cons.Invoke(new object[] { address, displayName, UInt32.MinValue });
            System.Net.Mail.MailAddress toAddressObj = (System.Net.Mail.MailAddress)obj;
            return toAddressObj;
        }
    }
}

No, you cannot turn that validation off.不,您不能关闭该验证。

EDIT:编辑:

After looking in to this a little bit it seems as if the following code snippet would be a feasible workaround:在稍微研究了一下之后,似乎下面的代码片段是一个可行的解决方法:

ConstructorInfo ctor = typeof(MailAddress).GetConstructor(
    BindingFlags.NonPublic | BindingFlags.Instance, null,
    new Type[] { typeof(string), typeof(string), typeof(string) }, null);

MailMessage msg = new MailMessage
{
    To = { (MailAddress)ctor.Invoke(new object[] { null, "[RFax:User", "/FN=0123456789]" }) }
};

There are two tricks here.这里有两个技巧。 The first one is to use the internal MailAddress constructor that doesn't parse/validate the supplied address.第一个是使用不解析/验证提供的地址的内部MailAddress构造函数。

The second trick is to split "fax address" on the @-sign and pass it as two parts ( user and domain ).第二个技巧是在 @-sign 上拆分“传真地址”并将其作为两部分(用户)传递。 This is needed because the SMTP To-header is later written by the framework using the MailAddress.Address property, and that property returns user + @ + domain .这是必需的,因为 SMTP To-header 稍后由框架使用MailAddress.Address属性编写,并且该属性返回user + @ + domain

Some thoughts...一些想法...

  • RFC 5322 requires that the email address be in the form local-part@domain . RFC 5322要求 email 地址采用local-part@domain形式。 You've omitted the @domain portion.您已经省略了@domain部分。

  • RFC 5322 further requires that the local-part be a dot-atom , consisting of 1 or more atoms separated by a single period (eg foo or foo.bar ). RFC 5322进一步要求local-part是一个dot-atom ,由一个或多个由单个句点分隔的atoms组成(例如foofoo.bar )。 An individual atom is a sequence of 1 or more of the following characters drawn from the US-ASCII (7-bit) character set of printable US-ASCII characters, excluding 'specials': meaning Upper-/lower-case letters, digits, and the characters单个atom是从可打印的 US-ASCII 字符的 US-ASCII(7 位)字符集中提取的 1 个或多个以下字符的序列,不包括“特殊”:表示大写/小写字母、数字、和人物

    ? # $ % & ' * + - / = ? ^ _ ` { | } ~

    Per the RFC, your email address is absolutely not legal — square brackets are "specials" in the grammar and thus not allowed.根据 RFC,您的 email 地址绝对不合法——方括号是语法中的“特殊”,因此是不允许的。

    If you want to use something other than a dot-atom as your local-part , then it must be a quoted-string , defined as a lead-in double-quote ( " ), followed by quoted-content , followed by a lead-out double-quote ( " ).如果你想使用dot-atom以外的东西作为你的local-part ,那么它必须是一个带quoted-string ,定义为前导双引号 ( " ),后跟quoted-content ,然后是前导-out 双引号 ( " )。 quoted-content is zero or more printable US-ASCII characters in the range 0x21–0x7E, excluding " and \ . quoted-content may also include insignificant "folding whitespace". \ , " and whitespace may be included by escaping them with a \ (eg, quotes are represented in the quoted string as \" , backslashes as \\ and spaces as \<sp> . quoted-content是 0x21–0x7E 范围内的零个或多个可打印的 US-ASCII 字符,不包括"\quoted-content还可能包含无关紧要的“折叠空格”。 \"并且 escaping 可以包含空格和\ (例如,引号在带引号的字符串中表示为\" ,反斜杠表示为\\ ,空格表示为\<sp>

Hope this helps!希望这可以帮助!

Edited to Note: Another option would be to send the mail directly via Exchange rather than via its SMTP front end, using the web services exposed by the Exchange Server: http://msdn.microsoft.com/en-us/library/bb204119.aspx .编辑注意:另一种选择是直接通过 Exchange 发送邮件,而不是通过其 SMTP 前端,使用由 Exchange Server 公开的 web 服务: Z80791B3AE7002CB88C246876D9FAA18C246876D9FAA18C246876D9FAA18C246876D9FAA18F0A9Z: .aspx

There are some open source smtp clients out there for.Net.对于.Net,有一些开源 smtp 客户端。 Most are old and stale, but you could just roll your own based on them, such as DotNetOpenMail大多数都是旧的和陈旧的,但你可以根据它们滚动你自己的,比如DotNetOpenMail

So, it appears you're using Diem Mail-to-Fax (PDF manual).因此,您似乎正在使用Diem Mail-to-Fax (PDF 手册)。 Note that the easiest way of doing this, is using the "SMTP IETF Addressing" section - which would simply require an MX record for fax.company.com, and SMTP compliant addresses such as:请注意,执行此操作的最简单方法是使用“SMTP IETF 寻址”部分 - 这将只需要传真.company.com 和 SMTP 兼容地址的 MX 记录,例如:

 fax=0123456789/pn=User@fax.company.com

You would then be able to send these faxes from any client, without having to go through your Exchange server.然后,您将能够从任何客户端发送这些传真,而无需通过 Exchange 服务器发送到 go。

The RFAX scheme requires special support from your Exchange server to route to the fax machine. RFAX方案需要 Exchange 服务器的特殊支持才能路由到传真机。 Since Outlook submits mail via MAPI, it can support this additional address spaces.由于 Outlook 通过 MAPI 提交邮件,它可以支持这个额外的地址空间。 I'm not entirely sure that even if you can get the SmtpClient to accept your address, that Exchange will know what to do with it when delivered via SMTP.我不完全确定即使您可以让SmtpClient接受您的地址,当通过 SMTP 交付时,Exchange 也会知道如何处理它。

I suspect that to use the RFAX scheme, you're going to have to submit the email via MAPI or Web Services, since it's not an SMTP address to begin with.我怀疑要使用 RFAX 方案,您将不得不通过 MAPI 或 Web 服务提交 email,因为它不是以 SMTP 开头的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM