Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SendGrid for C#发送文本/html编码为UTF-8的电子邮件?_C#_Email_Smtp_Sendgrid - Fatal编程技术网

如何使用SendGrid for C#发送文本/html编码为UTF-8的电子邮件?

如何使用SendGrid for C#发送文本/html编码为UTF-8的电子邮件?,c#,email,smtp,sendgrid,C#,Email,Smtp,Sendgrid,我试图发送包含特殊字符的电子邮件,如á、é、ó、í、ú等。我的代码如下所示: try { Mail message = Mail.GetInstance(); foreach (Users u in users) { message.AddBcc(u.Email); } message.From = new MailAddress(Microsoft.WindowsAzure.CloudConfigurationManager .GetS

我试图发送包含特殊字符的电子邮件,如á、é、ó、í、ú等。我的代码如下所示:

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;
}
string normalizedString = InString.Normalize(NormalizationForm.FormKD);
这是:

   message.Html = Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting
       ("emailText1") + " " + address+ ".<br/>" + 
       Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText2");
message.Html=Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting
(“emailText1”)+“”+地址+”
“+ Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting(“emailText2”);
但电子邮件仍以错误的编码发送

编辑2


我尝试了这个问题的答案,更具体地说,我尝试了方法
encodenonasicharacters
,但我在电子邮件
sanjer\u00c3\u00b3nimo Amanal
中得到了这个答案。我在这封邮件中采取了错误的方法吗?

这花了一些时间,但至少我找到了一种不用特殊字符发送电子邮件的方法,我必须使用规范化表单兼容性分解得到的字符串,这是通过字符串类'完成的,如下所示:

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;
}
string normalizedString = InString.Normalize(NormalizationForm.FormKD);
引用维基百科的话:

字符按兼容性和多重组合进行分解 字符按特定顺序排列


也就是说,如果还有一个字符的兼容性由两个字符表示,那么它将被所述字符替换。对于像é这样的字符,它将被e替换。

您可以定义消息的编码:

message.HtmlTransferEncoding = TransferEncoding.Base64;
此示例使ascii表的第二部分能够正确显示


快乐编码

我认为问题在于,尽管您可能已将数据存储在UTF8中,但将其读入.NET字符串将导致它转换回双字节。如果省略了meta字符集标记,它看起来正确吗?它仍然在没有正确编码的情况下发送消息