我可以关闭System.Net.Mail的电子邮件地址验证? [英] Can I turn off the email address validation in System.Net.Mail?

查看:718
本文介绍了我可以关闭System.Net.Mail的电子邮件地址验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想谈谈使用电子邮件到传真服务器软件。传真服务器将接受格式化SMTP邮件,他们隐蔽到的传真,并将其发送给在定义的解决传真号码。这已经被通过相同的服务器从Outlook发送电子邮件手动测试



下面是我的问题 - System.Net.Mail被抛出
System.FormatException:指定字符串不是一个电子邮件地址所要求的形式例外原因在于我试图发送到。

有什么办法,我可以关掉/改变这种验证,因为电子邮件地址可能不符合RFC,但它会工作,如果该电子邮件被发送



即我想发送给[RFax:用户名@ / FN = 0123456789]包括方括号



您可以将这个作为Outlook中的电子邮件地址



干杯
克里斯



修改



这是我使用绕过验证类的一个简化版本。这样做有它的两种方式 - 一个通过重写构造函数,并使用内部构造函数直接设置内部属性,其他。他们有略微不同的效果,如果有在电子邮件的地址空间



 使用系统; 
使用的System.Reflection;

命名空间邮件
{
公共类UnverifiedEmailAddress:System.Net.Mail.MailAddress
{
///<总结>
///构造绕过MailAddress
///&所述的验证; /总结>
///< PARAM NAME =地址>电子邮件地址创建< /参数>
公共UnverifiedEmailAddress(字符串的地址),
:基地(一@一)
{
字段信息字段= typeof运算(System.Net.Mail.MailAddress).GetField(地址BindingFlags.Instance | BindingFlags.NonPublic可);
field.SetValue(这一点,地址);
}

///<总结>
///静态方法来创建一个unverifed电子邮件地址绕过了地址验证
///< /总结>
///< PARAM NAME =地址>电子邮件地址创建< /参数>
///< PARAM NAME =显示名>作为电子邮件地址和LT显示名称; /参数>
///<&回报GT;< /回报>
私有静态System.Net.Mail.MailAddress GetUnverifiedEmailAddress(字符串的地址,字符串显示名)
{
ConstructorInfo利弊= typeof运算(System.Net.Mail.MailAddress).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic可,
空,
新型[] {typeof运算(字符串),typeof运算(字符串)的typeof(UInt32的)},
NULL);

obj对象= cons.Invoke(新的对象[] {地址,显示名,UInt32.MinValue});
System.Net.Mail.MailAddress toAddressObj =(System.Net.Mail.MailAddress)目标文件;
返回toAddressObj;
}
}
}


解决方案

没有,你不能把该验证关闭



编辑:



在寻找这一点后,它好像下面的代码片段将是一个可行的解决方法:

  ConstructorInfo男星= typeof运算(MailAddress).GetConstructor(
BindingFlags.NonPublic可| BindingFlags.Instance,空,
新型[] {typeof运算(字符串),typeof运算(字符串),typeof运算(字符串)},NULL);

味精MAILMESSAGE新= MAILMESSAGE
{
要= {(MailAddress)ctor.Invoke(新的对象[] {NULL,[RFax:用户,/ FN = 0123456789]})}
};



这里有两个窍门。第一种是使用内部 MailAddress 构造不解析/验证提供的地址。



第二招是在@ -sign分裂传真地址,并把它作为两个部分(用户的和的的)。这是必要的,因为SMTP要头是由框架使用 MailAddress.Address 属性后编写的,该属性返回用户的+ @ +的


I'm trying to talk to Fax server software using an 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. This has been manually tested by sending an email from Outlook via the same server.

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. exception due to the format of the email address that I am trying to send to

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

i.e. I want to send to [RFax:User@/FN=0123456789] including the square brackets

You can send to this as an e-mail address in Outlook

Cheers Chris

EDIT

This is a cut-down version of the class I'm using to bypass the validation. 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

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.

The second trick is to split "fax address" on the @-sign and pass it as two parts (user and domain). 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.

这篇关于我可以关闭System.Net.Mail的电子邮件地址验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆