MailAddress: An invalid character was found in the mail header

23,734

Solution 1

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.

Solution 2

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

Share:
23,734

Related videos on Youtube

Boguslaw Buczek
Author by

Boguslaw Buczek

I am a web developer/software engineer working currently in Healthcare for LucidHealth (https://lucidhealth.com). Developing on Azure in C#, .Net Core, ASP.NET Core, MVC, EF Core. Other side languages that come with the territory: TypeScript, JavaScript.

Updated on August 21, 2020

Comments

  • Boguslaw Buczek
    Boguslaw Buczek over 3 years

    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?