ASP.NET MailMessage.BodyEncoding and MailMessage.SubjectEncoding defaults

六月ゝ 毕业季﹏ 提交于 2019-12-10 23:12:28

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5307923/asp-net-mailmessage-bodyencoding-and-mailmessage-subjectencoding-defaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!