Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 836
Boguslaw Buczek Avatar asked Apr 26 '12 13:04

Boguslaw Buczek


2 Answers

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.

like image 141
RB. Avatar answered Oct 28 '22 12:10

RB.


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

like image 29
Wiktor Zychla Avatar answered Oct 28 '22 11:10

Wiktor Zychla