3

Simple question but I can't find the answer anywhere on MSDN...

Looking for the defaults ASP.NET will use for:

MailMessage.BodyEncoding and MailMessage.SubjectEncoding

If you don't set them in code?

Thanks

Oleks
  • 31,955
  • 11
  • 77
  • 132
timothyclifford
  • 6,799
  • 7
  • 57
  • 85

1 Answers1

8

For MailMessage.BodyEncoding MSDN says:

The value specified for the BodyEncoding property sets the character set field in the Content-Type header. The default character set is "us-ascii".

For MailMessage.SubjectEncoding I was also unable to find any documented default value, but reflector is to rescue:

internal void set_Subject(string value)
{
    if ((value != null) && MailBnfHelper.HasCROrLF(value))
    {
        throw new ArgumentException(SR.GetString("MailSubjectInvalidFormat"));
    }
    this.subject = value;
    if (((this.subject != null) && (this.subjectEncoding == null)) && 
         !MimeBasePart.IsAscii(this.subject, false))
    {
        this.subjectEncoding = Encoding.GetEncoding("utf-8");
    }
}

MimeBasePart.IsAscii is an internal method which tries to determine whether the passed value is in ASCII encoding:

internal static bool IsAscii(string value, bool permitCROrLF)
{
    if (value == null)
    {
        throw new ArgumentNullException("value");
    }
    foreach (char ch in value)
    {
        if (ch > '\x007f')
        {
            return false;
        }
        if (!permitCROrLF && ((ch == '\r') || (ch == '\n')))
        {
            return false;
        }
    }
    return true;
}

So it seems the default encoding for subject will be UTF-8 in the most cases.

Oleks
  • 31,955
  • 11
  • 77
  • 132
  • Aghhh! It must be documented, I couldn't understand why if US-ASCII is the default encoding, it was sending correctly accents and other special characters. The source code: https://referencesource.microsoft.com/#System/net/System/Net/mail/MailMessage.cs,8a4fd36de4145dc5,references – Marc May 29 '20 at 06:52