Can I turn off email address verification in System.Net.Mail?

I am trying to talk to fax server software using email. The fax server receives formatted SMTP messages and hides them for faxes and sends them to the fax number specified in the address. This was verified manually by sending an email from Outlook through the same server.

Here my problem is System.Net.Mail throws 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 it possible to disable / change this check because the email address may not meet the RFC requirements, but it will work if the email is sent.

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

You can send it as an email address in Outlook

Greetings Chris

EDIT

This is an abridged version of the class that I use to circumvent validation. There are two ways to do this: one by overriding the constructor and setting the internal attribute directly, and the other by using the 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; } } } 
+8
c # email
source share
4 answers

No, you cannot disable this check.

EDIT:

Having looked at it a bit, it seems that the following code fragment will be possible:

 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 is to use the internal MailAddress constructor, which does not parse / verify the supplied address.

The second trick is to split the “fax address” into an @ -sign and pass it as two parts (user and domain). This is necessary because SMTP To-header is later written in wireframe using the MailAddress.Address property, and this property returns the custom domain + @ +.

+7
source share

Some thoughts ...

  • RFC 5322 requires that the email address be in the form local-part @domain. You omitted the @domain part.

  • RFC 5322 additionally requires that the local part be a dot-atom consisting of 1 or more atoms separated by one period (for example, foo or foo.bar ). An individual atom is a sequence of 1 or more of the following characters, taken from the US-ASCII US-ASCII character set (7 bits), excluding "specials": values ​​of upper or lower case letters, numbers and characters

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

    At RFC, your email address is completely illegal; square brackets are “special” in grammar and thus are not allowed.

    If you want to use something other than dot-atom as your local-part , then it should be quoted-string , defined as the opening double quote ( " ), followed by quoted-content , and then display the double quote ( " ) . quoted-content is zero or more US-ASCII printable characters in the range 0x21 - 0x7E, excluding " and \ . quoted-content may also include minor" folding spaces ". \ , " , and spaces can be included by escaping them with \ (for example, quotation marks are represented in the quoted string as \" , backslash as \\ and spaces as \<sp> .

Hope this helps!

Edited for notes: Another option is to send mail directly through Exchange, and not through its SMTP interface, using the web services open by Exchange Server: http://msdn.microsoft.com/en-us/library/bb204119.aspx .

+2
source share

There are several open source smtp clients for .Net. Most of them are old and obsolete, but you can just use them yourself, for example DotNetOpenMail

+1
source share

So it looks like you are using Diem Mail-to-Fax (PDF manual). Note that the easiest way to do this is to use the “SMTP IETF Addressing” section, which simply requires MX records for fax.company.com and SMTP-compatible addresses, such as:

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

You can then send these faxes from any client without having to go through your Exchange server.

The RFAX scheme requires special support from your Exchange server for routing to the fax machine. Because Outlook sends mail through the MAPI, it can support this additional address space. I'm not quite sure that even if you can accept SmtpClient to accept your address, this Exchange will know what to do with it when delivered via SMTP.

I suspect that in order to use the RFAX scheme, you will need to send an email through MAPI or Web Services, as this is not an SMTP address to start with.

0
source share

All Articles