System.Net.Mail.MailMessage不符合RFC 2047,任何变通方法? [英] System.Net.Mail.MailMessage does not comply with RFC 2047, any workarounds?

查看:55
本文介绍了System.Net.Mail.MailMessage不符合RFC 2047,任何变通方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试解决.NET框架4.0版中的不合规问题。根据RFC 2047,在主题行中,编码的单词不能超过75个字符。如果是,则必须将其拆分为由CRLF SPACE分隔的多个编码字
。( http://www.ietf.org/rfc/rfc2047.txt  第3页,"2.编码词的语法")

I have been trying to workaround a non-compliancy issue within version 4.0 of the .NET framework. According to RFC 2047, in the subject line an encoded word can not be longer than 75 characters. If it is, it must be broken up into multiple encoded words separated by CRLF SPACE.(http://www.ietf.org/rfc/rfc2047.txt   Page 3, "2. Syntax of encoded-words")

如果使用MailMessage类并尝试使用CR或LF分配字符串,则会引发异常。在内部,它调用MailBnfHelper.HasCROrLF(value),如果为true,则抛出异常。

If you use the MailMessage class and try to assign a string with CR or LF, it throws an exception. Internally, it is calling MailBnfHelper.HasCROrLF(value) and if true it throws the exception.

在MailMessage类中,Subject实际定义为:

In the MailMessage class, the Subject is actually defined as:

私人留言信息;
$


当您设置MailMessage.Subject时,它实际上是:

private Message message;

When you set MailMessage.Subject, it actually does:

 public string Subject
   {
    get
    {
     if (this.message.Subject == null)
     {
      return string.Empty;
     }
     return this.message.Subject;
    }
    set
    {
     this.message.Subject = value;
    }
   }

在Message类中,当它设置主题字段,它将通过以下内容:

Within the Message class, when it sets the Subject field, it is going through the following:

  internal string Subject
   {
    get
    {
     return this.subject;
    }
    set
    {
     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");
    }
    }
   }

在Message类中,主题行最终定义为:

Within the Message class, the subject line is finally defined as:

 


private
 string
 subject

推荐答案

是的,看起来很糟糕:UTF-8将获得一个独特的Base64字符串,而其他编码(例如iso-8859) -1)创建一个字符串,虽然正确包装在~75个字符,但每行不使用编码字。

Yes, that looks pretty broken: UTF-8 will get a unique Base64 string, while other encodings (e.g. iso-8859-1) create a string that, while correctly wrapped at ~75 characters does not use an encoded word per line.

反射无效,因为MailMessage主题的内容仍然未编码;无论你设法写入什么,都会被编码,并且可能会破坏接收器甚至比长编码的单词更糟糕。实际上,
应该发生在内部MailWriter类的某个地方(有一个带有提示名称的方法"WriteAndFold")。作为内部,试图修补它将是有趣的(以消极的方式)。

Reflection will not help as the contents of a MailMessage subject are still unencoded; whatever you managed to write in there would just get encoded and possibly break the receiver even worse than a long encoded word ever would. The magic should actually happen somewhere in the internal MailWriter class (there is a method with the suggestive name "WriteAndFold"). Being internal, attempting to patch it is going to be interesting (in a negative way).

除非你遇到你提到的一些问题(我必须说我从来没有,并且我使用SmtpClient来对付各种服务器),我建议你在连接上提交一个bug,并希望它在下一次迭代中得到修复。

Unless you are experiencing some of the issues you mentioned (I must say I never had any, and I used the SmtpClient against all sorts of servers), I would advise you to file a bug on connect, and hope that it gets fixed in the next iteration.

对不起的消息感到抱歉

--mc

Sorry for the bad news
--mc


这篇关于System.Net.Mail.MailMessage不符合RFC 2047,任何变通方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆