Search code examples
c#.netemailexceptionmailaddress

MailAddress: An invalid character was found in the mail header


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.

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?


Solution

  • 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. "\ \\"").

    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.