简体   繁体   English

Mimekit 通过 Gmail 发送重音字符

[英]Mimekit sending accented characters through Gmail

I'm using C# and MimeKit (AE.Net.Mail) to send email through Gmail.我正在使用 C# 和 MimeKit (AE.Net.Mail) 通过 Gmail 发送 email。 Everything works perfectly, until I have an accented name in the body.一切都很完美,直到我在身体里有一个重音名字。

I have been unable to figure out how to send the accented characters properly.我一直无法弄清楚如何正确发送重音字符。 This is where I am with the code right now.这就是我现在使用代码的地方。 I've tried many dozens of iterations, and so far, nothing works.我已经尝试了几十次迭代,到目前为止,没有任何效果。 I've tried encoding in various formats, none of it worked, so I removed all of that for this example.我已经尝试过各种格式的编码,但都没有奏效,所以我在这个例子中删除了所有这些。 I want to reiterate.我想重申。 The email works perfectly, it's just the accented characters that cause an issue. email 完美运行,只是重音字符会导致问题。 I know it's related to encoding, but I just can't find the secret sauce to get it to work.我知道它与编码有关,但我只是找不到让它工作的秘诀。 (Note, the answer does need to work in all major mail clients) (注意,答案确实需要在所有主要的邮件客户端中工作)

var msg = new AE.Net.Mail.MailMessage
{
     Subject = "Hello Tést",
     From = new MailAddress("support@domain.com"),
     Sender = new MailAddress("support@domain.com"),
     Body = "Dear Tést, Thanks",
     ContentType = "text/html",
     Importance = AE.Net.Mail.MailPriority.Normal,
};
msg.ReplyTo.Add("support@domain.com");
var mimeMessage = MimeMessage.CreateFromMailMessage(msg);
var result = new GmailService(new BaseClientService.Initializer()
{
       HttpClientInitializer = GetCredentials("support@domain.com"),
       ApplicationName = "DomainApp",
})
.Users.Messages.Send(new Message
{
       Raw = urlSafeToBase64(mimeMessage.ToString())
},
"me");
var t = result.ExecuteAsync().GetAwaiter().GetResult();


private string urlSafeToBase64(string input)
{
    return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(input))
    .Replace('+', '-')
    .Replace('/', '_')
    .Replace("=", "");
}

This is the cause of the problem:这是问题的原因:

Raw = urlSafeToBase64(mimeMessage.ToString())

The ToString() method is not meant for doing stuff like this, it's only meant for debugging purposes. ToString() 方法不适合做这样的事情,它只用于调试目的。 See the docs: http://www.mimekit.net/docs/html/M_MimeKit_MimeMessage_ToString.htm请参阅文档: http://www.mimekit.net/docs/html/M_MimeKit_MimeMessage_ToString.htm

You will need to write the message to a Stream and then modify your urlSafeToBase64() method to either take a stream or a byte[]您需要将消息写入 Stream,然后修改 urlSafeToBase64() 方法以采用 stream 或字节 []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM