MailMessage.SubjectEncoding Property
.NET Framework 4
Gets or sets the encoding used for the subject content for this e-mail message.
Assembly: System (in System.dll)
The following code example demonstrates setting the SubjectEncoding property.
MailMessage message = new MailMessage(from, to); message.Body = "This is a test e-mail message sent by an application. "; // Include some non-ASCII characters in body and subject. string someArrows = new string(new char[] {'\u2190', '\u2191', '\u2192', '\u2193'}); message.Body += Environment.NewLine + someArrows; message.BodyEncoding = System.Text.Encoding.UTF8; message.Subject = "test message 1" + someArrows; message.SubjectEncoding = System.Text.Encoding.UTF8;
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Implementation doesn't conform to RFC2047
Note that the implementation of SubjectEncoding incorrectly leaves spaces un-encoded, whereas RFC 2047 explicitly forbids this. For example, the subject line "Your login details" when encoded with message.SubjectEncoding = System.Text.Encoding.UTF8; will become "=?utf-8?Q?Your login details?=". The correct encoded version should be "=?utf-8?Q?Your=20login=20details?=".
As a result some email systems will reject emails which use this SubjectEncoding property.
One solution is to replace spaces with non-breaking spaces before assigning the string to the message subject:
string subjectLine = "Subject of message";
message.SubjectEncoding = Encoding.UTF8;
message.Subject = subjectLine.Replace(' ', '\xA0');
As a result some email systems will reject emails which use this SubjectEncoding property.
One solution is to replace spaces with non-breaking spaces before assigning the string to the message subject:
string subjectLine = "Subject of message";
message.SubjectEncoding = Encoding.UTF8;
message.Subject = subjectLine.Replace(' ', '\xA0');