示例#1
0
 /// <include file='../../docs.xml'
 /// path='docs/doc[@name="M:PeterO.Mail.MediaTypeBuilder.#ctor(PeterO.Mail.MediaType)"]/*'/>
 public MediaTypeBuilder(MediaType mt) {
   if (mt == null) {
     throw new ArgumentNullException("mt");
   }
   this.parameters = new Dictionary<string, string>(mt.Parameters);
   this.type = mt.TopLevelType;
   this.subtype = mt.SubType;
 }
示例#2
0
 /// <include file='../../docs.xml'
 /// path='docs/doc[@name="M:PeterO.Mail.Message.#ctor"]/*'/>
 public Message() {
   this.headers = new List<string>();
   this.parts = new List<Message>();
   this.body = new byte[0];
   this.contentType = MediaType.TextPlainUtf8;
   this.headers.Add("message-id");
   this.headers.Add(this.GenerateMessageID());
   this.headers.Add("from");
   this.headers.Add("*****@*****.**");
   this.headers.Add("mime-version");
   this.headers.Add("1.0");
 }
示例#3
0
 /// <include file='../../docs.xml'
 /// path='docs/doc[@name="M:PeterO.Mail.Message.SetTextBody(System.String)"]/*'/>
 public Message SetTextBody(string str) {
   if (str == null) {
     throw new ArgumentNullException("str");
   }
   this.body = DataUtilities.GetUtf8Bytes(str, true, true);
   this.contentType = IsShortAndAllAscii(str) ? MediaType.TextPlainAscii :
     MediaType.TextPlainUtf8;
   return this;
 }
示例#4
0
   /// <include file='../../docs.xml'
   /// path='docs/doc[@name="M:PeterO.Mail.Message.SetTextAndHtml(System.String,System.String)"]/*'/>
   public Message SetTextAndHtml(string text, string html) {
     if (text == null) {
       throw new ArgumentNullException("text");
     }
     if (html == null) {
       throw new ArgumentNullException("html");
     }
     // The spec for multipart/alternative (RFC 2046) says that
     // the fanciest version of the message should go last (in
     // this case, the HTML version)
     var textMessage = new Message().SetTextBody(text);
     var htmlMessage = new Message().SetHtmlBody(html);
     this.contentType =
 MediaType.Parse("multipart/alternative; boundary=\"=_Boundary00000000\"");
     IList<Message> messageParts = this.Parts;
     messageParts.Clear();
     messageParts.Add(textMessage);
     messageParts.Add(htmlMessage);
     return this;
   }
示例#5
0
 /// <include file='../../docs.xml'
 /// path='docs/doc[@name="M:PeterO.Mail.Message.SetHeader(System.Int32,System.String,System.String)"]/*'/>
 public Message SetHeader(int index, string name, string value) {
   if (index < 0) {
     throw new ArgumentException("index (" + index + ") is less than " +
                 "0");
   }
   if (index >= (this.headers.Count / 2)) {
     throw new ArgumentException("index (" + index +
                 ") is not less than " + (this.headers.Count
                 / 2));
   }
   name = ValidateHeaderField(name, value);
   this.headers[index * 2] = name;
   this.headers[(index * 2) + 1] = value;
   if (name.Equals("content-type")) {
     this.contentType = MediaType.Parse(value);
   } else if (name.Equals("content-disposition")) {
     this.contentDisposition = ContentDisposition.Parse(value);
   }
   return this;
 }
示例#6
0
 /// <include file='../../docs.xml'
 /// path='docs/doc[@name="M:PeterO.Mail.Message.RemoveHeader(System.String)"]/*'/>
 public Message RemoveHeader(string name) {
   if (name == null) {
     throw new ArgumentNullException("name");
   }
   name = DataUtilities.ToLowerCaseAscii(name);
   // Remove the header field
   for (int i = 0; i < this.headers.Count; i += 2) {
     if (this.headers[i].Equals(name)) {
       this.headers.RemoveAt(i);
       this.headers.RemoveAt(i);
       i -= 2;
     }
   }
   if (name.Equals("content-type")) {
     this.contentType = MediaType.TextPlainAscii;
   } else if (name.Equals("content-disposition")) {
     this.contentDisposition = null;
   }
   return this;
 }
示例#7
0
 /// <include file='../../docs.xml'
 /// path='docs/doc[@name="M:PeterO.Mail.Message.RemoveHeader(System.Int32)"]/*'/>
 public Message RemoveHeader(int index) {
   if (index < 0) {
     throw new ArgumentException("index (" + index + ") is less than " +
                 "0");
   }
   if (index >= (this.headers.Count / 2)) {
     throw new ArgumentException("index (" + index +
                 ") is not less than " + (this.headers.Count
                 / 2));
   }
   string name = this.headers[index * 2];
   this.headers.RemoveAt(index * 2);
   this.headers.RemoveAt(index * 2);
   if (name.Equals("content-type")) {
     this.contentType = MediaType.TextPlainAscii;
   } else if (name.Equals("content-disposition")) {
     this.contentDisposition = null;
   }
   return this;
 }
示例#8
0
    private void ProcessHeaders(bool assumeMime, bool digest) {
      var haveContentType = false;
      bool mime = assumeMime;
      var haveContentDisp = false;
      string transferEncodingValue = String.Empty;
      for (int i = 0; i < this.headers.Count; i += 2) {
        string name = this.headers[i];
        string value = this.headers[i + 1];
        if (name.Equals("content-transfer-encoding")) {
          int startIndex = HeaderParser.ParseCFWS(value, 0, value.Length, null);
          // NOTE: Actually "token", but all known transfer encoding values
          // fit the same syntax as the stricter one for top-level types and
          // subtypes
          int endIndex = MediaType.SkipMimeTypeSubtype(
            value,
            startIndex,
            value.Length,
            null);
          transferEncodingValue = (
            HeaderParser.ParseCFWS(
              value,
              endIndex,
              value.Length,
              null) == value.Length) ? value.Substring(
  startIndex,
  endIndex - startIndex) : String.Empty;
        }
        mime |= name.Equals("mime-version");
        if (value.IndexOf("=?", StringComparison.Ordinal) >= 0) {
          IHeaderFieldParser parser = HeaderFieldParsers.GetParser(name);
          // Decode encoded words in the header field where possible
          value = parser.DecodeEncodedWords(value);
          this.headers[i + 1] = value;
        }
      }
      this.contentType = digest ? MediaType.MessageRfc822 :
        MediaType.TextPlainAscii;
      var haveInvalid = false;
      var haveContentEncoding = false;
      for (int i = 0; i < this.headers.Count; i += 2) {
        string name = this.headers[i];
        string value = this.headers[i + 1];
        if (mime && name.Equals("content-transfer-encoding")) {
          value = DataUtilities.ToLowerCaseAscii(transferEncodingValue);
          this.headers[i + 1] = value;
          if (value.Equals("7bit")) {
            this.transferEncoding = EncodingSevenBit;
          } else if (value.Equals("8bit")) {
            this.transferEncoding = EncodingEightBit;
          } else if (value.Equals("binary")) {
            this.transferEncoding = EncodingBinary;
          } else if (value.Equals("quoted-printable")) {
            this.transferEncoding = EncodingQuotedPrintable;
          } else if (value.Equals("base64")) {
            this.transferEncoding = EncodingBase64;
          } else {
            // Unrecognized transfer encoding
            this.transferEncoding = EncodingUnknown;
          }
          haveContentEncoding = true;
        } else if (mime && name.Equals("content-type")) {
          if (haveContentType) {
            // DEVIATION: If there is already a content type,
            // treat content type as application/octet-stream
            if (haveInvalid || MediaType.Parse(value, null) == null) {
              this.contentType = MediaType.TextPlainAscii;
              haveInvalid = true;
            } else {
              this.contentType = MediaType.ApplicationOctetStream;
            }
          } else {
            this.contentType = MediaType.Parse(
              value,
              null);
            if (this.contentType == null) {
              this.contentType = digest ? MediaType.MessageRfc822 :
                MediaType.TextPlainAscii;
              haveInvalid = true;
            }
            haveContentType = true;
          }
        } else if (mime && name.Equals("content-disposition")) {
          if (haveContentDisp) {
            string valueExMessage = "Already have this header: " + name;
#if DEBUG
            valueExMessage += "[old=" + this.contentType + ", new=" + value +
              "]";
            valueExMessage = valueExMessage.Replace("\r\n", " ");
#endif
            throw new MessageDataException(valueExMessage);
          }
          this.contentDisposition = ContentDisposition.Parse(value);
          haveContentDisp = true;
        }
      }
      if (this.transferEncoding == EncodingUnknown) {
        this.contentType = MediaType.Parse("application/octet-stream");
      }
      if (!haveContentEncoding && this.contentType.TypeAndSubType.Equals(
        "message/rfc822")) {
        // DEVIATION: Be a little more liberal with rfc822
        // messages with 8-bit bytes
        this.transferEncoding = EncodingEightBit;
      }
      if (this.transferEncoding == EncodingSevenBit) {
        string charset = this.contentType.GetCharset();
        if (charset.Equals("utf-8")) {
          // DEVIATION: Be a little more liberal with UTF-8
          this.transferEncoding = EncodingEightBit;
        } else if (this.contentType.TypeAndSubType.Equals("text/html")) {
          if (charset.Equals("us-ascii") || charset.Equals("ascii") ||
              charset.Equals("windows-1252") || charset.Equals(
                "windows-1251") ||
              (charset.Length > 9 && charset.Substring(0, 9).Equals(
                "iso-8859-"))) {
            // DEVIATION: Be a little more liberal with text/html and
            // single-byte charsets or UTF-8
            this.transferEncoding = EncodingEightBit;
          }
        }
      }
      if (this.transferEncoding == EncodingQuotedPrintable ||
          this.transferEncoding == EncodingBase64 ||
          this.transferEncoding == EncodingUnknown) {
        if (this.contentType.IsMultipart ||
            (this.contentType.TopLevelType.Equals("message") &&
             !this.contentType.SubType.Equals("global") &&
             !this.contentType.SubType.Equals("global-headers") &&
             !this.contentType.SubType.Equals(
               "global-disposition-notification") &&
             !this.contentType.SubType.Equals("global-delivery-status"))) {
          if (this.transferEncoding == EncodingQuotedPrintable) {
            // DEVIATION: Treat quoted-printable for multipart and message
            // as 7bit instead
            this.transferEncoding = EncodingSevenBit;
          } else {
            string exceptText =
              "Invalid content encoding for multipart or message";
#if DEBUG
            exceptText += " [type=" + this.contentType + "]";
#endif
            throw new MessageDataException(exceptText);
          }
        }
      }
    }
示例#9
0
 /// <include file='../../docs.xml'
 /// path='docs/doc[@name="M:PeterO.Mail.MediaType.Parse(System.String,PeterO.Mail.MediaType)"]/*'/>
 public static MediaType Parse(string str, MediaType defaultValue) {
   if (str == null) {
     throw new ArgumentNullException("str");
   }
   MediaType mt = ParseMediaType(str);
   return mt ?? defaultValue;
 }
示例#10
0
    /// <include file='../../docs.xml'
    /// path='docs/doc[@name="M:PeterO.Mail.MediaType.Parse(System.String,PeterO.Mail.MediaType)"]/*'/>
    public static MediaType Parse(string str, MediaType defaultValue) {
      if (str == null) {
        throw new ArgumentNullException("str");
      }
      var mt = new MediaType();
      mt.parameters = new Dictionary<string, string>();
      if (!mt.ParseMediaType(str)) {
        #if DEBUG
        // Console.WriteLine("Unparsable: " + str);
        #endif
return defaultValue;
      }
      return mt;
    }