MailAddress: An invalid character was found in the mail header
-
03-06-2021 - |
Question
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\\FAXReportCover.rtf@fax.ourcompanymail.com
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\\"FAXReportCover.rtf@fax.ourcompanymail.com
I think it would be ok.
OTHER TIPS
Years ago I found a workaround over a similar issue. The issue was in the DisplayName parameter in the constructor of MailAddress.
Maybe it will help you too:
http://netpl.blogspot.com/2008/09/smtpclient-exception-invalid-character.html
I received this error when if this is modified to "network" --- then error got resolved. ( My understanding is - Incase of specified pickupdirectory option, the header -encoding utf-8 (base64) was giving error )
Hope it helps