当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - ASP.NET MailMessage.BodyEncoding and MailMessage.SubjectEncoding defaults

问题描述:

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

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: