2

First, I do not know if my issue come from a bad use of Mailkit (that is a great piece of software, by the way) or a miss-configuration of smtp.office365.com.

Version of MailKit is: 2.6.0

I try to send a message with this string Test d'ênvoïe d'èmail avec des àccénts, in the subject and text body.

When I use the smtp.office365.com account to send the email, when I received it in my office 365 box, the subject is fine, but the body look like this Test d'�nvo�e d'�mail avec des �cc�nts.

I try to use another smtp.office365.com account from another company, and when I received it in my office 365 box, the subject and the body are fine.

I try to send the same email with the first smtp account to a GMAIL email box. The subject and the body were fine.

So I don’t know if the first smtp account is miss-configured, or outlook (desktop and web) are buggy ?

The code that sends the email is quite simple and nothing fancy is used. This code is used every day to send emails from our software, and this is the first time we have this kind of problem with a smtp. What bothers me is that it’s an office 365 smtp, like the one we use every day without any trouble.

--- EDIT As requested by @jstedfast here is the raw MIME generated by MimeKit/MailKit

From: email@domain.com
Date: Tue, 26 May 2020 14:16:13 +0000
Subject: Test =?utf-8?b?ZCfDqm52b8OvZSBkJ8OobWFpbCBhdmVjIGRlcyDDoGNjw6ludHM=?=
Message-Id: <54G3D036QAU4.3G654QDV2MVS2@4b245b0f83f4>
To: some@email.com
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="=-xjuWE0kh6/lmMc7JIcMJ3g=="

--=-xjuWE0kh6/lmMc7JIcMJ3g==
Content-Type: text/plain; charset=utf-8

Test d'ênvoïe d'èmail avec des àccénts ça, ~ $ #{][*µµ$£= +
--=-xjuWE0kh6/lmMc7JIcMJ3g==
Content-Type: text/html; charset=utf-8
Content-Id: <RUL3D036QAU4.Y7K3UPECLV0Q3@4b245b0f83f4>

Test d'ênvoïe d'èmail avec des àccénts ça, ~ $ #{][*µµ$£= +
--=-xjuWE0kh6/lmMc7JIcMJ3g==--
Waldo
  • 1,070
  • 1
  • 11
  • 30
  • 1
    You are using special characters so you need to encode the string with : System.Net.WebUtility.HtmlEncode(string). Then decode on receiving side : System.Net.WebUtility.HtmlDecode(string). If this is not the issue then either you are using Text mode to send which only handles ASCII characters (unless you change the default encoding) or You do not have a font installed that handles the character set (encoding) that is used in the message. I've seen all 3 type of issues and it is hard to distinguish which is causing this issue. – jdweng May 26 '20 at 09:47
  • No, do NOT encode using HtmlEncode/Decode - that's for HTML, not email, which uses different encoding rules. – jstedfast May 26 '20 at 12:59

3 Answers3

1

As jdweng suggests in his comment, it could be a font issue in the receiving Outlook client (in other words, the font being used by Outlook does not have glyphs for ê, ï, è, à, or é).

Since you are not getting 2 garbage symbols like �� (or À�) per character, that suggests that either MailKit is using a character encoding like iso-8859-1 (or iso-8859-15?) and that the receiving Outlook client is also using a single-byte character encoding to decode it... or else MailKit is using UTF-8 and the receiving Outlook client is properly recognizing it as UTF-8 (UTF-8 uses 2 bytes per accent character in the Latin alphabets), but cannot display the glyphs due to a font issue.

If you haven't explicitly set a character encoding for MailKit to use in the message body, then it defaults to UTF-8 (which suggests a font issue).

This is an example of how you might specify a character encoding to use in MimeKit/MailKit:

var body = new TextPart ("plain");
body.SetText ("Test d'ênvoïe d'èmail avec des àccénts", Encoding.GetEncoding ("iso-8859-1"));

So I guess this leaves us with the question of how are you creating the message (and in particular, the message body)?

If you can share the raw MIME generated by MimeKit/MailKit, that would be helpful.

To do that, you can use the MimeMessage.WriteTo(string fileName) method and then use your favorite text editor to open the file and copy & paste the contents.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • I've edit my question to add the raw MIME generated by MimeKit/MailKit, – Waldo May 26 '20 at 14:30
  • 2
    Okay, here's my suggestion: before sending the message via SmtpClient.Send()/SendAsync(), call `message.Prepare (EncodingConstraint.SevenBit);` – jstedfast May 26 '20 at 14:31
1

As explained by jstedfast, the solution to this idiotic problem is to add message.Prepare (EncodingConstraint.SevenBit); before sending the email.

So thank to jstedfast, for the solution ! You saved my day.

Waldo
  • 1,070
  • 1
  • 11
  • 30
0

If you get settings from appsetting.json, try to save it with encoding, I also added Enconding.UTF8 in MailBoxAddres of From

        var mimeMessage = new MimeMessage();
        mimeMessage.From.Add(new MailboxAddress(Encoding.UTF8, _emailSettings.SenderName, _emailSettings.SenderEmail));

That solved my problem.

Arthur B
  • 192
  • 2
  • 8