/// <summary> /// Parses body from the specified stream /// </summary> /// <param name="owner">Owner MIME entity.</param> /// <param name="defaultContentType">Default content-type for this body.</param> /// <param name="stream">Stream from where to read body.</param> /// <returns>Returns parsed body.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b>, <b>defaultContentType</b> or <b>strean</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when any parsing errors.</exception> protected static new MIME_b Parse(MIME_Entity owner,MIME_h_ContentType defaultContentType,SmartStream stream) { if(owner == null){ throw new ArgumentNullException("owner"); } if(defaultContentType == null){ throw new ArgumentNullException("defaultContentType"); } if(stream == null){ throw new ArgumentNullException("stream"); } string mediaType = null; try{ mediaType = owner.ContentType.TypeWithSubtype; } catch{ mediaType = "unparsable/unparsable"; } MIME_b_Unknown retVal = new MIME_b_Unknown(mediaType); Net_Utils.StreamCopy(stream,retVal.EncodedStream,32000); return retVal; }
/// <summary> /// Default constructor. /// </summary> /// <param name="contentType">Content type.</param> /// <exception cref="ArgumentNullException">Is raised when <b>contentType</b> is null reference.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> public MIME_b_MultipartParallel(MIME_h_ContentType contentType) : base(contentType) { if(!string.Equals(contentType.TypeWithSubtype,"multipart/parallel",StringComparison.CurrentCultureIgnoreCase)){ throw new ArgumentException("Argument 'contentType.TypeWithSubype' value must be 'multipart/parallel'."); } }
/// <summary> /// Parses body from the specified stream /// </summary> /// <param name="owner">Owner MIME entity.</param> /// <param name="defaultContentType">Default content-type for this body.</param> /// <param name="stream">Stream from where to read body.</param> /// <returns>Returns parsed body.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b>, <b>mediaType</b> or <b>stream</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when any parsing errors.</exception> protected static new MIME_b Parse(MIME_Entity owner,MIME_h_ContentType defaultContentType,SmartStream stream) { if(owner == null){ throw new ArgumentNullException("owner"); } if(defaultContentType == null){ throw new ArgumentNullException("defaultContentType"); } if(stream == null){ throw new ArgumentNullException("stream"); } MIME_b_Text retVal = null; if(owner.ContentType != null){ retVal = new MIME_b_Text(owner.ContentType.TypeWithSubtype); } else{ retVal = new MIME_b_Text(defaultContentType.TypeWithSubtype); } Net_Utils.StreamCopy(stream,retVal.EncodedStream,32000); retVal.SetModified(false); return retVal; }
/// <summary> /// Default constructor. /// </summary> /// <param name="contentType">Content type.</param> /// <exception cref="ArgumentNullException">Is raised when <b>contentType</b> is null reference.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> public MIME_b(MIME_h_ContentType contentType) { if(contentType == null){ throw new ArgumentNullException("contentType"); } m_pContentType = contentType; }
/// <summary> /// Default constructor. /// </summary> /// <param name="contentType">Content type.</param> /// <exception cref="ArgumentNullException">Is raised when <b>contentType</b> is null reference.</exception> public MIME_b_SinglepartBase(MIME_h_ContentType contentType) : base(contentType) { if(contentType == null){ throw new ArgumentNullException("contentType"); } m_pEncodedDataStream = new MemoryStreamEx(32000); }
/// <summary> /// Default constructor. /// </summary> /// <param name="contentType">Content type.</param> /// <exception cref="ArgumentNullException">Is raised when <b>contentType</b> is null reference.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> public MIME_b_MultipartReport(MIME_h_ContentType contentType) : base(contentType) { if(!string.Equals(contentType.TypeWithSubtype,"multipart/report",StringComparison.CurrentCultureIgnoreCase)){ throw new ArgumentException("Argument 'contentType.TypeWithSubype' value must be 'multipart/report'."); } //if(contentType.Parameters["report-type"] == null){ // throw new ArgumentException("Argument 'contentType' does not contain required 'report-type' paramter."); //} }
/// <summary> /// Default constructor. /// </summary> /// <param name="contentType">Content type.</param> /// <exception cref="ArgumentNullException">Is raised when <b>contentType</b> is null reference.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> public MIME_b_Multipart(MIME_h_ContentType contentType) : base(contentType) { if(contentType == null){ throw new ArgumentNullException("contentType"); } if(string.IsNullOrEmpty(contentType.Param_Boundary)){ throw new ArgumentException("Argument 'contentType' doesn't contain required boundary parameter."); } m_pContentType = contentType; m_pBodyParts = new MIME_EntityCollection(); }
/// <summary> /// Parses body from the specified stream /// </summary> /// <param name="owner">Owner MIME entity.</param> /// <param name="defaultContentType">Default content-type for this body.</param> /// <param name="stream">Stream from where to read body.</param> /// <returns>Returns parsed body.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b>, <b>defaultContentType</b> or <b>stream</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when any parsing errors.</exception> protected static new MIME_b Parse(MIME_Entity owner,MIME_h_ContentType defaultContentType,SmartStream stream) { if(owner == null){ throw new ArgumentNullException("owner"); } if(defaultContentType == null){ throw new ArgumentNullException("defaultContentType"); } if(stream == null){ throw new ArgumentNullException("stream"); } if(owner.ContentType == null || owner.ContentType.Param_Boundary == null){ throw new ParseException("Multipart entity has not required 'boundary' paramter."); } MIME_b_MultipartParallel retVal = new MIME_b_MultipartParallel(owner.ContentType); ParseInternal(owner,owner.ContentType.TypeWithSubtype,stream,retVal); return retVal; }
/// <summary> /// Parses MIME entiry from the specified stream. /// </summary> /// <param name="stream">Source stream.</param> /// <param name="headerEncoding">Header reading encoding. If not sure UTF-8 is recommended.</param> /// <param name="defaultContentType">Default content type.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b>,<b>headerEncoding</b> or <b>defaultContentType</b> is null reference.</exception> protected internal void Parse(SmartStream stream,Encoding headerEncoding,MIME_h_ContentType defaultContentType) { if(stream == null){ throw new ArgumentNullException("stream"); } if(headerEncoding == null){ throw new ArgumentNullException("headerEncoding"); } if(defaultContentType == null){ throw new ArgumentNullException("defaultContentType"); } m_pHeader.Parse(stream,headerEncoding); m_pBody = m_pBodyProvider.Parse(this,stream,defaultContentType); m_pBody.SetParent(this,false); }
/// <summary> /// Parses MIME entity body from specified stream. /// </summary> /// <param name="owner">Owner MIME entity.</param> /// <param name="stream">Stream from where to parse entity body.</param> /// <param name="defaultContentType">Default content type.</param> /// <returns>Returns parsed body.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>owner</b>, <b>strean</b> or <b>defaultContentType</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when header field parsing errors.</exception> public MIME_b Parse(MIME_Entity owner,SmartStream stream,MIME_h_ContentType defaultContentType) { if(owner == null){ throw new ArgumentNullException("owner"); } if(stream == null){ throw new ArgumentNullException("stream"); } if(defaultContentType == null){ throw new ArgumentNullException("defaultContentType"); } string mediaType = defaultContentType.TypeWithSubtype; try{ if(owner.ContentType != null){ mediaType = owner.ContentType.TypeWithSubtype; } } catch{ // Do nothing, content will be MIME_b_Unknown. mediaType = "unknown/unknown"; } Type bodyType = null; // We have exact body provider for specified mediaType. if(m_pBodyTypes.ContainsKey(mediaType)){ bodyType = m_pBodyTypes[mediaType]; } // Use default mediaType. else{ // Registered list of mediaTypes are available: http://www.iana.org/assignments/media-types/. string mediaRootType = mediaType.Split('/')[0].ToLowerInvariant(); if(mediaRootType == "application"){ bodyType = typeof(MIME_b_Application); } else if(mediaRootType == "audio"){ bodyType = typeof(MIME_b_Audio); } else if(mediaRootType == "image"){ bodyType = typeof(MIME_b_Image); } else if(mediaRootType == "message"){ bodyType = typeof(MIME_b_Message); } else if(mediaRootType == "multipart"){ bodyType = typeof(MIME_b_Multipart); } else if(mediaRootType == "text"){ bodyType = typeof(MIME_b_Text); } else if(mediaRootType == "video"){ bodyType = typeof(MIME_b_Video); } else{ bodyType = typeof(MIME_b_Unknown); } } return (MIME_b)bodyType.GetMethod("Parse",System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.FlattenHierarchy).Invoke(null,new object[]{owner,defaultContentType,stream}); }
/// <summary> /// Parses header field from the specified value. /// </summary> /// <param name="value">Header field value. Header field name must be included. For example: 'Content-Type: text/plain'.</param> /// <returns>Returns parsed header field.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>value</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when header field parsing errors.</exception> public static MIME_h_ContentType Parse(string value) { if(value == null){ throw new ArgumentNullException("value"); } // We should not have encoded words here, but some email clients do this, so encoded them if any. value = MIME_Encoding_EncodedWord.DecodeS(value); MIME_h_ContentType retVal = new MIME_h_ContentType(); string[] name_value = value.Split(new char[]{':'},2); if(name_value.Length != 2){ throw new ParseException("Invalid Content-Type: header field value '" + value + "'."); } MIME_Reader r = new MIME_Reader(name_value[1]); string type = r.Token(); if(type == null){ throw new ParseException("Invalid Content-Type: header field value '" + value + "'."); } retVal.m_Type = type; if(r.Char(false) != '/'){ throw new ParseException("Invalid Content-Type: header field value '" + value + "'."); } string subtype = r.Token(); if(subtype == null){ throw new ParseException("Invalid Content-Type: header field value '" + value + "'."); } retVal.m_SubType = subtype; if(r.Available > 0){ retVal.m_pParameters.Parse(r); } retVal.m_ParseValue = value; retVal.m_IsModified = false; return retVal; }
private void updateMessage(Mail_Message m) { if (from != null) m.From = Mail_t_MailboxList.Parse(from.getString("account")); m.To = Mail_t_AddressList.Parse(txtTo.Text); if (!String.IsNullOrWhiteSpace(txtSubject.Text)) m.Subject = txtSubject.Text; if (!String.IsNullOrWhiteSpace(txtCc.Text)) m.Cc = Mail_t_AddressList.Parse(txtCc.Text.Trim()); if (Receipt.IsChecked == true) { m.DispositionNotificationTo = Mail_t_MailboxList.Parse(from.getString("account")); } if (Importance.IsChecked == true) { m.Priority = "urgent"; } string sHtmlText = htmlEditor.ContentHtml; List<string> sUrlList = getHtmlImageUrlList(sHtmlText); if (sUrlList.Count > 0 || attachments.Children.OfType<AttachmentItem>().Count() > 0) { //--- multipart/mixed ------------------------------------------------------------------------------------------------- MIME_h_ContentType contentType_multipartMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed); contentType_multipartMixed.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.'); MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed(contentType_multipartMixed); m.Body = multipartMixed; //--- multipart/alternative ----------------------------------------------------------------------------------------- MIME_Entity entity_mulipart_alternative = new MIME_Entity(); MIME_h_ContentType contentType_multipartAlternative = new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative); contentType_multipartAlternative.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.'); MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative(contentType_multipartAlternative); entity_mulipart_alternative.Body = multipartAlternative; multipartMixed.BodyParts.Add(entity_mulipart_alternative); //--- text/plain ---------------------------------------------------------------------------------------------------- MIME_Entity entity_text_plain = new MIME_Entity(); MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain); entity_text_plain.Body = text_plain; text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, txtEditor.Text); multipartAlternative.BodyParts.Add(entity_text_plain); // Create attachment etities: --- applactation/octet-stream ------------------------- foreach (AttachmentItem item in attachments.Children.OfType<AttachmentItem>()) { multipartMixed.BodyParts.Add(Mail_Message.CreateAttachment(item.File)); } foreach (string url in sUrlList) { MIME_Entity img_entity = Mail_Message.CreateImage(url); string contentID = Guid.NewGuid().ToString().Replace("-", "$"); img_entity.ContentID = "<" + contentID + ">"; sHtmlText = sHtmlText.Replace(url, "cid:" + contentID); multipartMixed.BodyParts.Add(img_entity); } //--- text/html ------------------------------------------------------------------------------------------------------ MIME_Entity entity_text_html = new MIME_Entity(); MIME_b_Text text_html = new MIME_b_Text(MIME_MediaTypes.Text.html); entity_text_html.Body = text_html; text_html.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, sHtmlText); multipartAlternative.BodyParts.Add(entity_text_html); } else { //--- multipart/alternative ----------------------------------------------------------------------------------------- MIME_h_ContentType contentType_multipartAlternative = new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative); contentType_multipartAlternative.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.'); MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative(contentType_multipartAlternative); m.Body = multipartAlternative; //--- text/plain ---------------------------------------------------------------------------------------------------- MIME_Entity entity_text_plain = new MIME_Entity(); MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain); entity_text_plain.Body = text_plain; text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, txtEditor.Text); multipartAlternative.BodyParts.Add(entity_text_plain); //--- text/html ------------------------------------------------------------------------------------------------------ MIME_Entity entity_text_html = new MIME_Entity(); MIME_b_Text text_html = new MIME_b_Text(MIME_MediaTypes.Text.html); entity_text_html.Body = text_html; text_html.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, sHtmlText); multipartAlternative.BodyParts.Add(entity_text_html); } }
/// <summary> /// Parses body from the specified stream /// </summary> /// <param name="owner">Owner MIME entity.</param> /// <param name="defaultContentType">Default content-type for this body.</param> /// <param name="stream">Stream from where to read body.</param> /// <returns>Returns parsed body.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b>, <b>defaultContentType</b> or <b>strean</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when any parsing errors.</exception> protected static new MIME_b Parse(MIME_Entity owner,MIME_h_ContentType defaultContentType,SmartStream stream) { if(owner == null){ throw new ArgumentNullException("owner"); } if(defaultContentType == null){ throw new ArgumentNullException("defaultContentType"); } if(stream == null){ throw new ArgumentNullException("stream"); } MIME_b_ApplicationPkcs7Mime retVal = new MIME_b_ApplicationPkcs7Mime(); Net_Utils.StreamCopy(stream,retVal.EncodedStream,32000); return retVal; }
/// <summary> /// Parses body from the specified stream /// </summary> /// <param name="owner">Owner MIME entity.</param> /// <param name="defaultContentType">Default content-type for this body.</param> /// <param name="stream">Stream from where to read body.</param> /// <returns>Returns parsed body.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b>, <b>defaultContentType</b> or <b>stream</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when any parsing errors.</exception> protected static MIME_b Parse(MIME_Entity owner,MIME_h_ContentType defaultContentType,SmartStream stream) { if(owner == null){ throw new ArgumentNullException("owner"); } if(defaultContentType == null){ throw new ArgumentNullException("defaultContentType"); } if(stream == null){ throw new ArgumentNullException("stream"); } throw new NotImplementedException("Body provider class does not implement required Parse method."); }
/// <summary> /// Parses body from the specified stream /// </summary> /// <param name="owner">Owner MIME entity.</param> /// <param name="defaultContentType">Default content-type for this body.</param> /// <param name="stream">Stream from where to read body.</param> /// <returns>Returns parsed body.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b>, <b>defaultContentType</b> or <b>stream</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when any parsing errors.</exception> protected static new MIME_b Parse(MIME_Entity owner,MIME_h_ContentType defaultContentType,SmartStream stream) { if(owner == null){ throw new ArgumentNullException("owner"); } if(defaultContentType == null){ throw new ArgumentNullException("defaultContentType"); } if(stream == null){ throw new ArgumentNullException("stream"); } // We need to buffer all body data, otherwise we don't know if we have readed all data // from stream. MemoryStream msBuffer = new MemoryStream(); Net_Utils.StreamCopy(stream,msBuffer,32000); msBuffer.Position = 0; SmartStream parseStream = new SmartStream(msBuffer,true); MIME_b_MessageDeliveryStatus retVal = new MIME_b_MessageDeliveryStatus(); //Pare per-message fields. retVal.m_pMessageFields.Parse(parseStream); // Parse per-recipient fields. while(parseStream.Position - parseStream.BytesInReadBuffer < parseStream.Length){ MIME_h_Collection recipientFields = new MIME_h_Collection(new MIME_h_Provider()); recipientFields.Parse(parseStream); retVal.m_pRecipientBlocks.Add(recipientFields); } return retVal; }
/// <summary> /// 发送阅读回折邮件 /// </summary> /// <param name="sHtmlText">邮件内容</param> /// <param name="from">发送人</param> /// <param name="to">接收人</param> public void sendReceiptMail(string sHtmlText, string subject, ASObject from, string[] to) { using (MemoryStreamEx stream = new MemoryStreamEx(32000)) { Mail_Message m = new Mail_Message(); m.MimeVersion = "1.0"; m.Date = DateTime.Now; m.MessageID = MIME_Utils.CreateMessageID(); m.Subject = subject; StringBuilder sb = new StringBuilder(); foreach (string p in to) { if (sb.Length > 0) sb.Append(","); sb.Append(p); } m.To = Mail_t_AddressList.Parse(sb.ToString()); //--- multipart/alternative ----------------------------------------------------------------------------------------- MIME_h_ContentType contentType_multipartAlternative = new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative); contentType_multipartAlternative.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.'); MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative(contentType_multipartAlternative); m.Body = multipartAlternative; //--- text/plain ---------------------------------------------------------------------------------------------------- MIME_Entity entity_text_plain = new MIME_Entity(); MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain); entity_text_plain.Body = text_plain; text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, sHtmlText); multipartAlternative.BodyParts.Add(entity_text_plain); //--- text/html ------------------------------------------------------------------------------------------------------ MIME_Entity entity_text_html = new MIME_Entity(); MIME_b_Text text_html = new MIME_b_Text(MIME_MediaTypes.Text.html); entity_text_html.Body = text_html; text_html.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, sHtmlText); multipartAlternative.BodyParts.Add(entity_text_html); MIME_Encoding_EncodedWord headerwordEncoder = new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8); m.ToStream(stream, headerwordEncoder, Encoding.UTF8); stream.Position = 0; SMTP_Client.QuickSendSmartHost(null, from.getString("send_address", "stmp.sina.com"), from.getInt("send_port", 25), from.getBoolean("is_send_ssl", false), from.getString("account"), PassUtil.Decrypt(from.getString("password")), from.getString("account"), to, stream); } }
/// <summary> /// Parses body from the specified stream /// </summary> /// <param name="owner">Owner MIME entity.</param> /// <param name="defaultContentType">Default content-type for this body.</param> /// <param name="stream">Stream from where to read body.</param> /// <returns>Returns parsed body.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b>, <b>defaultContentType</b> or <b>stream</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when any parsing errors.</exception> protected static new MIME_b Parse(MIME_Entity owner,MIME_h_ContentType defaultContentType,SmartStream stream) { if(owner == null){ throw new ArgumentNullException("owner"); } if(defaultContentType == null){ throw new ArgumentNullException("defaultContentType"); } if(stream == null){ throw new ArgumentNullException("stream"); } MIME_b_MessageRfc822 retVal = new MIME_b_MessageRfc822(); retVal.m_pMessage = Mail_Message.ParseFromStream(stream); return retVal; }