0
votes

Referring to the MIME Encoded-word syntax on wikipedia:

=?charset?encoding?encoded text?=

I want to specify both charset and encoding for my MailAddress.DisplayName, MailMessage.Subject etc.

For examples:

  • charset ISO-2022-JP with Q encoding: =?iso-2022-jp?Q?=5B=1B=24B=21A=1B=28B=5D=5B=1B=24B=21B=1B=28B=5D?=
  • charset ISO-2022-JP with Base64 encoding: =?ISO-2022-JP?B?WxskQiFBGyhCXVs=?=

It seems like current .NET framework implementation does not allow this flexibility. The encoding-related properties (such as MailMessage.SubjectEncoding) maps to MIME charset, and there is no way to specify MIME encoding. The MIME encoding is set by the framework like the following:

If the MailMessage.SubjectEncoding is set to UTF-8 (the default), UTF-32, Unicode or BigEndianUnicode AND the subject contains non-ASCII characters, then it will be encoded in Base64. Otherwise it will be encoded in Quoted Printable. (Source: Microsoft Connect)

If using MailMessage class is not possible, is there any other way to achieve this?

1

1 Answers

0
votes

There's no way to do it for System.Net.Mail, but you could use a library like MimeKit.

In MimeKit, you can specify the charset for any header, but it has its own logic for picking the appropriate encoding to use because pretty much no one cares enough to want to set it themselves. There's a pretty easy mathematical formula to decide the most efficient encoding algorithm, so there's generally no point in overriding it.

However, if you really need/want to override it, since MimeKit is full open source, you could just modify the source code.