我正在尝试发送包含 á、é、ó、í、ú 等特殊字符的电子邮件。我的代码如下所示:
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;
}
我试图在我发送的 Html 中指定字符集,但它不起作用,而不是发送 this San Jerónimo Amanal
,这是正在发送的内容San Jerónimo Amanal
。
如何以正确的编码发送它?任何帮助将不胜感激。
编辑
我尝试了这两种方法:
message.Html = "<meta charset=\"UTF-8\"/>" +
Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText1") +
" " + address+ ".<br/>" +
Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText2");
和这个:
message.Html = Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting
("emailText1") + " " + address+ ".<br/>" +
Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText2");
但是电子邮件仍然以错误的编码发送。
编辑 2
我尝试了这个问题的答案Converting Unicode strings to escaped ascii string,更具体地说,我尝试了该方法EncodeNonAsciiCharacters
,但我在电子邮件中得到了这个San Jer\u00c3\u00b3nimo Amanal
。我在这个中采取了错误的方法吗?