Merge pull request #3626 from lateralusX/jlorenss/win-api-family-support-eglib
[mono.git] / mcs / class / System / System.Net.Mail / MailMessage.cs
index 45720eb92a82372f56a6d2145868b77561c38ba5..b5a58d86b8160caadfdf41d028408ffe441e0983 100644 (file)
@@ -145,7 +145,7 @@ namespace System.Net.Mail {
                }
 
                internal TransferEncoding ContentTransferEncoding {
-                       get { return ContentType.GuessTransferEncoding (BodyEncoding); }
+                       get { return GuessTransferEncoding (BodyEncoding); }
                }
 
                public Encoding BodyEncoding {
@@ -153,6 +153,11 @@ namespace System.Net.Mail {
                        set { bodyEncoding = value; }
                }
 
+               public TransferEncoding BodyTransferEncoding {
+                       get { return GuessTransferEncoding (BodyEncoding); }
+                       set { throw new NotImplementedException (); }
+               }
+
                public MailAddressCollection CC {
                        get { return cc; }
                }
@@ -181,28 +186,18 @@ namespace System.Net.Mail {
                        set { priority = value; }
                }
 
-#if NET_4_0
                public
-#else
-               internal
-#endif
                Encoding HeadersEncoding {
                        get { return headersEncoding; }
                        set { headersEncoding = value; } 
                }
 
-#if NET_4_0
                public
-#else
-               internal
-#endif
                MailAddressCollection ReplyToList {
                        get { return replyTo; }
                }
 
-#if NET_4_0
                [Obsolete ("Use ReplyToList instead")]
-#endif
                public MailAddress ReplyTo {
                        get {
                                if (replyTo.Count == 0)
@@ -254,9 +249,63 @@ namespace System.Net.Mail {
 
                private Encoding GuessEncoding (string s)
                {
-                       return ContentType.GuessEncoding (s);
+                       for (int i = 0; i < s.Length; i++)
+                               if (s [i] >= '\u0080')
+                                       return UTF8Unmarked;
+                       return null;
+               }
+
+               internal static TransferEncoding GuessTransferEncoding (Encoding enc)
+               {
+                       if (Encoding.ASCII.Equals (enc))
+                               return TransferEncoding.SevenBit;
+                       else if (Encoding.UTF8.CodePage == enc.CodePage ||
+#if !MOBILE
+                           Encoding.Unicode.CodePage == enc.CodePage || Encoding.UTF32.CodePage == enc.CodePage
+#else
+                           Encoding.Unicode.CodePage == enc.CodePage
+#endif
+                                        )
+                               return TransferEncoding.Base64;
+                       else
+                               return TransferEncoding.QuotedPrintable;
+               }
+
+               static char [] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+               internal static string To2047(byte [] bytes)
+               {
+                       StringBuilder sb = new StringBuilder ();
+                       foreach (byte i in bytes) {
+                               if (i < 0x21 || i > 0x7E || i == '?' || i == '=' || i == '_') {
+                                       sb.Append ('=');
+                                       sb.Append (hex [(i >> 4) & 0x0f]);
+                                       sb.Append (hex [i & 0x0f]);
+                               } else
+                                       sb.Append ((char) i);
+                       }
+                       return sb.ToString ();
                }
 
+               internal static string EncodeSubjectRFC2047 (string s, Encoding enc)
+               {
+                       if (s == null || Encoding.ASCII.Equals (enc))
+                               return s;
+                       for (int i = 0; i < s.Length; i++)
+                               if (s [i] >= '\u0080') {
+                                       string quoted = To2047(enc.GetBytes (s));
+                                       return String.Concat ("=?", enc.HeaderName, "?Q?", quoted, "?=");
+                               }
+                       return s;
+               }
+
+               static Encoding utf8unmarked;
+               static Encoding UTF8Unmarked {
+                       get {
+                               if (utf8unmarked == null)
+                                       utf8unmarked = new UTF8Encoding (false);
+                               return utf8unmarked;
+                       }
+               }
                #endregion // Methods
        }
 }