3

I'm trying to send emails that contains special characters like á, é, ó, í, ú, etc. My code looks like this:

try
{
   Mail message = Mail.GetInstance();
   foreach (Users u in users)
   {
       message.AddBcc(u.Email);
   }
   message.From = new MailAddress(Microsoft.WindowsAzure.CloudConfigurationManager
       .GetSetting("emailFrom"), Microsoft.WindowsAzure.CloudConfigurationManager
       .GetSetting("emailFromName"));
   message.Subject = Microsoft.WindowsAzure.CloudConfigurationManager
       .GetSetting("emailSubject");

   message.Html = "<meta charset=\"UTF-8\"/>" + 
       Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText1") + 
       " " + address+ ".<br/>" + 
       Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText2");

   var transport = SMTP.GetInstance(new NetworkCredential(
       Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailLogin"), 
       Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailPass")));
   transport.Deliver(message);
   isEmailSent = true;
}
catch (Exception ex)
{
    Debug.WriteLine(ex.Message + " \n " + ex.InnerException + " \n " + ex.StackTrace);
    isEmailSent = false;
}

I tried to specify the charset in the Html I'm sending but it doesn't work, instead of sending this San Jerónimo Amanal, this is what is being sent San Jerónimo Amanal.

How can I send it in the correct encoding? Any help will be appreciated.

EDIT

I tried this two approches:

   message.Html = "<meta charset=\"UTF-8\"/>" + 
       Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText1") + 
       " " + address+ ".<br/>" + 
       Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText2");

And this:

   message.Html = Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting
       ("emailText1") + " " + address+ ".<br/>" + 
       Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText2");

But the email is still being sent with the wrong encoding.

EDIT 2

I tried the answer on this question Converting Unicode strings to escaped ascii string, more especifically, I tried the method EncodeNonAsciiCharacters but I got this in the email San Jer\u00c3\u00b3nimo Amanal. Did I take the wrong approach in this one?

Community
  • 1
  • 1
Uriel Arvizu
  • 1,876
  • 6
  • 37
  • 97
  • I think the problem is that although you may have stored your data in UTF8, reading it into a .NET string will cause it to be converted back to double-byte. If you omit the meta charset tag, does it look right? – jlew Jul 09 '13 at 17:18
  • It still sends the message without the proper encoding – Uriel Arvizu Jul 09 '13 at 17:27

1 Answers1

3

Well it took some time, but at least I found a way to send the email without the special characters, I had to normalize the string I got with Normalization Form Compatibility Decomposition, which is done through the String class' Normalize method like this:

string normalizedString = InString.Normalize(NormalizationForm.FormKD);

With this, quoting from wikipedia:

characters are decomposed by compatibility, and multiple combining characters are arranged in a specific order.

Meaning if there's also a character whose compability is represented by two characters, then it will be replace by said characters. With characters like é, it will be replaced by e.

Community
  • 1
  • 1
Uriel Arvizu
  • 1,876
  • 6
  • 37
  • 97