我正在研究一個應用程序,它可以發送帶有附件的電子郵件,它可以工作,直到我嘗試特殊字符æ,ø,å。無法編碼特殊字符的電子郵件以正確的格式,gmail api&ae.net.mail
我打得有點左右測試不同的編碼,它看起來像的主題是ISO-8859-1進行編碼,而郵件的休息UTF-8編碼。
這裏是我的方法產生一個谷歌Gmail API消息
public Message CreateMessage(string to, string from, string body, string subject, GmailService service, string[] files = null, string bcc = null)
{
AE.Net.Mail.MailMessage message = new AE.Net.Mail.MailMessage()
{
Subject = subject,
Body = body,
From = new MailAddress(from),
};
message.To.Add(new MailAddress(to));
message.ReplyTo.Add(message.From);
message.Headers.Add("Content-Type", "text/plain; charset=utf-8");
if (bcc != null)
message.Bcc.Add(new MailAddress(bcc));
if (files != null)
{
foreach(string file in files)
{
using (var opennedFile = File.Open(file, FileMode.Open, FileAccess.Read))
using (MemoryStream stream = new MemoryStream())
{
string[] FileName = file.Split('\\');
opennedFile.CopyTo(stream);
message.Attachments.Add(new AE.Net.Mail.Attachment(stream.ToArray(), MediaTypeNames.Application.Octet, FileName[FileName.Length - 1], true));
}
}
}
var msgStr = new StringWriter();
message.Save(msgStr);
return new Message() {
Raw = Base64UrlEncode(msgStr.ToString()),
};
}
private static string Base64UrlEncode(string message)
{
var inputBytes = Encoding.GetEncoding("utf-8").GetBytes(message);
return Convert.ToBase64String(inputBytes).Replace('+', '-').Replace('/', '_').Replace("=", "");
}
message.ContentType =「text/plain的;字符集= UTF-8」不解決這個問題,使附加的文件顯示在body Base64
其中是從截屏? – DaImTo