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; } } }
Chris gill
source share