无法以正确的格式,gmail api&编码特殊字符的电子邮件ae.net.mail [英] unable to encode special characters email in right format, gmail api & ae.net.mail

查看:139
本文介绍了无法以正确的格式,gmail api&编码特殊字符的电子邮件ae.net.mail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个可以发送带有附件的电子邮件并且可以正常工作的应用程序,直到我尝试使用特殊字符æ,ø,å.

I'm working on an application that can send emails out with attachments and it works, until I try special characters æ, ø, å.

我测试了不同的编码方式,看起来主题是在ISO-8859-1中编码的,而其余邮件则是在UTF-8中编码的.

I played a bit around testing different encodings and it looks like the subject is being encoded in ISO-8859-1 while the rest of the mail is encoded in UTF-8.

这是我生成Google Gmail API消息的方法

Here is my method that generates a Google Gmail API message

        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; charset = utf-8"不能解决此问题,并使附件在正文中显示为Base64

message.ContentType = "text/plain; charset=utf-8" does not fix this issue and makes the attached files show in the body as Base64

推荐答案

您可以使用以下技术 在主题标头中使用UTF-8.

You could use the following technique to use UTF-8 in the subject header.

=?charset?encoding?encoded-text?=

然后,您可以使用 charset = utf-8 encoding = B (B = base64),并将编码后的主题编码为 encoded-text

You could then use charset=utf-8, encoding=B (B = base64), and encoded subject as encoded-text.

示例

Subject: =?utf-8?B?aGVsbG8=?= // 'aGVsbG8=' is 'hello' in base64 format.

这篇关于无法以正确的格式,gmail api&编码特殊字符的电子邮件ae.net.mail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆