//public MailMessage GenMail(string mailSubject) //{ // // 生成邮件 // MailMessage message = new MailMessage("*****@*****.**", "*****@*****.**"); // message.Sender = new MailAddress("*****@*****.**"); // message.Subject = mailSubject; // message.Body = "*****@*****.**" + " Send " + mailSubject; // message.Priority = MailPriority.High; // return message; //} public int SendMail(MailAccount Sender, MailAccount Receiver, string mailSubject) { Mail_Message msg = Create_PlainText_Html_Attachment_Image(Receiver.UserName, Sender.MailAddress, Sender.UserName); MemoryStream m = new MemoryStream(); MIME_Encoding_EncodedWord ew = new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.B, Encoding.UTF8); msg.ToStream(m, ew, Encoding.UTF8, false); m.Position = 0; LumiSoft.Net.SMTP.Client.SMTP_Client smtpClient; smtpClient = new LumiSoft.Net.SMTP.Client.SMTP_Client(); try { smtpClient.Connect(Sender.SmtpHost, Sender.SendPort, Sender.SendSsl); try { smtpClient.EhloHelo(Sender.SmtpHost); smtpClient.Authenticate(Sender.UserName, Sender.Password); smtpClient.MailFrom(Sender.UserName, -1); smtpClient.RcptTo(Receiver.UserName); smtpClient.SendMessage(m); } finally { smtpClient.Disconnect(); } } finally { smtpClient = null; } return 0; }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> protected internal override void ToStream(Stream stream,MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset,bool headerReencode) { if(stream == null){ throw new ArgumentNullException("stream"); } m_pMessage.ToStream(stream,headerWordEncoder,headerParmetersCharset,headerReencode); }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> internal protected override void ToStream(Stream stream,MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset,bool headerReencode) { if(stream == null){ throw new ArgumentNullException("stream"); } Net_Utils.StreamCopy(GetEncodedDataStream(),stream,32000); }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <param name="reEncode">If true always specified encoding is used. If false and header field value not modified, original encoding is kept.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder,Encoding parmetersCharset,bool reEncode) { if(string.IsNullOrEmpty(m_Address)){ return "Return-Path: <>\r\n"; } else{ return "Return-Path: <" + m_Address + ">\r\n"; } }
/// <summary> /// Stores MIME entity to the specified file. /// </summary> /// <param name="file">File name with path where to store MIME entity.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>file</b> is null.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> public void ToFile(string file,MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset,bool headerReencode) { if(file == null){ throw new ArgumentNullException("file"); } if(file == ""){ throw new ArgumentException("Argument 'file' value must be specified."); } using(FileStream fs = File.Create(file)){ ToStream(fs,headerWordEncoder,headerParmetersCharset,headerReencode); } }
/// <summary> /// Returns address as string value. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <returns>Returns address as string value.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder) { if(string.IsNullOrEmpty(m_DisplayName)){ return "<" + m_Address + ">"; } else{ if(MIME_Encoding_EncodedWord.MustEncode(m_DisplayName)){ return wordEncoder.Encode(m_DisplayName) + " " + "<" + m_Address + ">"; } else{ return TextUtils.QuoteString(m_DisplayName) + " " + "<" + m_Address + ">"; } } }
/// <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; retVal.m_pParameters.Parse(r); retVal.m_ParseValue = value; retVal.m_IsModified = false; return(retVal); }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <param name="reEncode">If true always specified encoding is used. If false and header field value not modified, original encoding is kept.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset, bool reEncode) { if (!reEncode && m_ParseValue != null) { return(m_ParseValue); } else { if (wordEncoder != null) { return(m_Name + ": " + wordEncoder.Encode(m_Value) + "\r\n"); } else { return(m_Name + ": " + m_Value + "\r\n"); } } }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> internal protected override void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset, bool headerReencode) { if (stream == null) { throw new ArgumentNullException("stream"); } /* RFC 2046 5.1.1. * NOTE: The CRLF preceding the boundary delimiter line is conceptually * attached to the boundary so that it is possible to have a part that * does not end with a CRLF (line break). */ // Set "preamble" text if any. if (!string.IsNullOrEmpty(m_TextPreamble)) { byte[] preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble); stream.Write(preableBytes, 0, preableBytes.Length); } for (int i = 0; i < m_pBodyParts.Count; i++) { MIME_Entity bodyPart = m_pBodyParts[i]; // Start new body part. byte[] bStart = Encoding.UTF8.GetBytes("\r\n--" + this.Entity.ContentType.Param_Boundary + "\r\n"); stream.Write(bStart, 0, bStart.Length); bodyPart.ToStream(stream, headerWordEncoder, headerParmetersCharset, headerReencode); // Last body part, close boundary. if (i == (m_pBodyParts.Count - 1)) { byte[] bEnd = Encoding.UTF8.GetBytes("\r\n--" + this.Entity.ContentType.Param_Boundary + "--\r\n"); stream.Write(bEnd, 0, bEnd.Length); } } // Set "epilogoue" text if any. if (!string.IsNullOrEmpty(m_TextEpilogue)) { byte[] epilogoueBytes = Encoding.UTF8.GetBytes(m_TextEpilogue); stream.Write(epilogoueBytes, 0, epilogoueBytes.Length); } }
/// <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_Unstructured Parse(string value) { if (value == null) { throw new ArgumentNullException("value"); } MIME_h_Unstructured retVal = new MIME_h_Unstructured(); string[] name_value = value.Split(new char[] { ':' }, 2); if (name_value[0].Trim() == string.Empty) { throw new ParseException("Invalid header field '" + value + "' syntax."); } retVal.m_Name = name_value[0]; retVal.m_Value = MIME_Encoding_EncodedWord.DecodeS(MIME_Utils.UnfoldHeader(name_value.Length == 2 ? name_value[1].TrimStart() : "")); retVal.m_ParseValue = value; return(retVal); }
/// <summary> /// Returns address as string value. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <returns>Returns address as string value.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder) { StringBuilder retVal = new StringBuilder(); if(string.IsNullOrEmpty(m_DisplayName)){ retVal.Append(":"); } else{ if(MIME_Encoding_EncodedWord.MustEncode(m_DisplayName)){ retVal.Append(wordEncoder.Encode(m_DisplayName) + ":"); } else{ retVal.Append(TextUtils.QuoteString(m_DisplayName) + ":"); } } for(int i=0;i<m_pList.Count;i++){ retVal.Append(m_pList[i].ToString(wordEncoder)); if(i < (m_pList.Count - 1)){ retVal.Append(","); } } retVal.Append(";"); return retVal.ToString(); }
/// <summary> /// Stores MIME entity to the specified file. /// </summary> /// <param name="file">File name with path where to store MIME entity.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>file</b> is null.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> public void ToFile(string file,MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset) { ToFile(file,headerWordEncoder,headerParmetersCharset,false); }
/// <summary> /// Returns MIME entity as byte[]. /// </summary> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <returns>Returns MIME entity as byte[].</returns> public byte[] ToByte(MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset) { return ToByte(headerWordEncoder,headerParmetersCharset,false); }
/// <summary> /// Returns MIME entity as string. /// </summary> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <returns>Returns MIME entity as string.</returns> public string ToString(MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset) { return ToString(headerWordEncoder,headerParmetersCharset,false); }
/// <summary> /// Store MIME enity to the specified stream. /// </summary> /// <param name="stream">Stream where to store MIME entity. Storing starts form stream current position.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null.</exception> public void ToStream(Stream stream,MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset) { ToStream(stream,headerWordEncoder,headerParmetersCharset,false); }
/// <summary> /// Stores MIME entity to the specified file. /// </summary> /// <param name="file">File name with path where to store MIME entity.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>file</b> is null.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> public void ToFile(string file, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { ToFile(file, headerWordEncoder, headerParmetersCharset, false); }
/// <summary> /// Returns MIME entity as string. /// </summary> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <returns>Returns MIME entity as string.</returns> public string ToString(MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { return(ToString(headerWordEncoder, headerParmetersCharset, false)); }
/// <summary> /// Constructs ENVELOPE address structure. /// </summary> /// <param name="address">Mailbox address.</param> /// <param name="wordEncoder">Unicode words encoder.</param> /// <returns></returns> private static string ConstructAddress(Mail_t_Mailbox address,MIME_Encoding_EncodedWord wordEncoder) { /* An address structure is a parenthesized list that describes an electronic mail address. The fields of an address structure are in the following order: personal name, [SMTP] at-domain-list (source route), mailbox name, and host name. */ // NOTE: all header fields and parameters must in ENCODED form !!! StringBuilder retVal = new StringBuilder(); retVal.Append("("); // personal name if(address.DisplayName != null){ retVal.Append(TextUtils.QuoteString(wordEncoder.Encode(RemoveCrlf(address.DisplayName)))); } else{ retVal.Append("NIL"); } // source route, always NIL (not used nowdays) retVal.Append(" NIL"); // mailbox name retVal.Append(" " + TextUtils.QuoteString(wordEncoder.Encode(RemoveCrlf(address.LocalPart)))); // host name if(address.Domain != null){ retVal.Append(" " + TextUtils.QuoteString(wordEncoder.Encode(RemoveCrlf(address.Domain)))); } else{ retVal.Append(" NIL"); } retVal.Append(")"); return retVal.ToString(); }
/// <summary> /// Returns MIME header as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="reEncode">If true always specified encoding is used. If false and header fields which value not modified, original encoding is kept.</param> /// <returns>Returns MIME header as string.</returns> public string ToString(MIME_Encoding_EncodedWord wordEncoder,Encoding parmetersCharset,bool reEncode) { StringBuilder retVal = new StringBuilder(); foreach(MIME_h field in m_pFields){ retVal.Append(field.ToString(wordEncoder,parmetersCharset,reEncode)); } return retVal.ToString(); }
/// <summary> /// Stores header to the specified stream. /// </summary> /// <param name="stream">Stream where to store header.</param> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="reEncod">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> public void ToStream(Stream stream,MIME_Encoding_EncodedWord wordEncoder,Encoding parmetersCharset,bool reEncod) { if(stream == null){ throw new ArgumentNullException("stream"); } byte[] header = Encoding.UTF8.GetBytes(ToString(wordEncoder,parmetersCharset,reEncod)); stream.Write(header,0,header.Length); }
/// <summary> /// Returns MIME header as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <returns>Returns MIME header as string.</returns> public string ToString(MIME_Encoding_EncodedWord wordEncoder,Encoding parmetersCharset) { return ToString(wordEncoder,parmetersCharset,false); }
/// <summary> /// Stores header to the specified stream. /// </summary> /// <param name="stream">Stream where to store header.</param> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> public void ToStream(Stream stream,MIME_Encoding_EncodedWord wordEncoder,Encoding parmetersCharset) { ToStream(stream,wordEncoder,parmetersCharset,false); }
/// <summary> /// Returns header as byte[] data. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <returns>Returns header as byte[] data.</returns> public byte[] ToByte(MIME_Encoding_EncodedWord wordEncoder,Encoding parmetersCharset) { using(MemoryStream ms = new MemoryStream()){ ToStream(ms,wordEncoder,parmetersCharset); ms.Position = 0; return ms.ToArray(); } }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded. /// If encoding needed, UTF-8 is strongly reccomended if not sure.</param> /// <param name="reEncode">If true always specified encoding is used. If false and header field value not modified, original encoding is kept.</param> /// <returns>Returns header field as string.</returns> public abstract string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset, bool reEncode);
public static string DecodeField(string encodedString) { var wordEncoder = new LumiSoft.Net.MIME.MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8); return(wordEncoder.Decode(encodedString)); }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <param name="reEncode">If true always specified encoding is used. If false and header field value not modified, original encoding is kept.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder,Encoding parmetersCharset,bool reEncode) { if(reEncode || this.IsModified){ StringBuilder retVal = new StringBuilder(); retVal.Append("Received: "); retVal.Append("from " + m_From); if(m_pFrom_TcpInfo != null){ retVal.Append(" (" + m_pFrom_TcpInfo.ToString() + ")"); } retVal.Append(" by " + m_By); if(m_pBy_TcpInfo != null){ retVal.Append(" (" + m_pBy_TcpInfo.ToString() + ")"); } if(!string.IsNullOrEmpty(m_Via)){ retVal.Append(" via " + m_Via); } if(!string.IsNullOrEmpty(m_With)){ retVal.Append(" with " + m_With); } if(!string.IsNullOrEmpty(m_ID)){ retVal.Append(" id " + m_ID); } if(!string.IsNullOrEmpty(m_For)){ retVal.Append(" for " + m_For); } retVal.Append("; " + MIME_Utils.DateTimeToRfc2822(m_Time)); retVal.Append("\r\n"); /* Some servers doesnt like uppercase. retVal.Append("Received: "); retVal.Append("FROM " + m_From); if(m_pFrom_TcpInfo != null){ retVal.Append(" (" + m_pFrom_TcpInfo.ToString() + ")"); } retVal.Append(" BY " + m_By); if(m_pBy_TcpInfo != null){ retVal.Append(" (" + m_pBy_TcpInfo.ToString() + ")"); } if(!string.IsNullOrEmpty(m_Via)){ retVal.Append(" VIA " + m_Via); } if(!string.IsNullOrEmpty(m_With)){ retVal.Append(" WITH " + m_With); } if(!string.IsNullOrEmpty(m_ID)){ retVal.Append(" ID " + m_ID); } if(!string.IsNullOrEmpty(m_For)){ retVal.Append(" FOR " + m_For); } retVal.Append("; " + MIME_Utils.DateTimeToRfc2822(m_Time)); retVal.Append("\r\n");*/ return retVal.ToString(); } else{ return m_ParseValue; } }
/// <summary> /// Constructs ENVELOPE addresses structure. /// </summary> /// <param name="mailboxes">Mailboxes.</param> /// <param name="wordEncoder">Unicode words encoder.</param> /// <returns></returns> private static string ConstructAddresses(Mail_t_Mailbox[] mailboxes,MIME_Encoding_EncodedWord wordEncoder) { StringBuilder retVal = new StringBuilder(); retVal.Append("("); foreach(Mail_t_Mailbox address in mailboxes){ retVal.Append(ConstructAddress(address,wordEncoder)); } retVal.Append(")"); return retVal.ToString(); }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <param name="reEncode">If true always specified encoding is used. If false and header field value not modified, original encoding is kept.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder,Encoding parmetersCharset,bool reEncode) { return m_ParseValue; }
/// <summary> /// Store MIME enity to the specified stream. /// </summary> /// <param name="stream">Stream where to store MIME entity. Storing starts form stream current position.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null.</exception> public void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { ToStream(stream, headerWordEncoder, headerParmetersCharset, false); }
/// <summary> /// Returns header field parameters as string. /// </summary> /// <param name="charset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <returns>Returns header field parameters as string.</returns> public string ToString(Encoding charset) { /* RFC 2231. * If parameter conatins 8-bit byte, we need to encode parameter value * If parameter value length bigger than MIME maximum allowed line length, * we need split value. */ if (charset == null) { charset = Encoding.Default; } StringBuilder retVal = new StringBuilder(); foreach (MIME_h_Parameter parameter in this.ToArray()) { if (string.IsNullOrEmpty(parameter.Value)) { retVal.Append(";\r\n\t" + parameter.Name); } // We don't need to encode or split value. else if ((charset == null || Net_Utils.IsAscii(parameter.Value)) && parameter.Value.Length < 76) { retVal.Append(";\r\n\t" + parameter.Name + "=" + TextUtils.QuoteString(parameter.Value)); } // Use RFC 2047 to encode parameter. else if (m_EncodeRfc2047) { retVal.Append(";\r\n\t" + parameter.Name + "=" + TextUtils.QuoteString(MIME_Encoding_EncodedWord.EncodeS(MIME_EncodedWordEncoding.B, Encoding.UTF8, false, parameter.Value))); } // We need to RFC 2231 encode/split value. else { byte[] byteValue = charset.GetBytes(parameter.Value); List <string> values = new List <string>(); // Do encoding/splitting. int offset = 0; char[] valueBuff = new char[50]; foreach (byte b in byteValue) { // We need split value as RFC 2231 says. if (offset >= (50 - 3)) { values.Add(new string(valueBuff, 0, offset)); offset = 0; } // Normal char, we don't need to encode. if (MIME_Reader.IsAttributeChar((char)b)) { valueBuff[offset++] = (char)b; } // We need to encode byte as %X2. else { valueBuff[offset++] = '%'; valueBuff[offset++] = (b >> 4).ToString("X")[0]; valueBuff[offset++] = (b & 0xF).ToString("X")[0]; } } // Add pending buffer value. if (offset > 0) { values.Add(new string(valueBuff, 0, offset)); } for (int i = 0; i < values.Count; i++) { // Only fist value entry has charset and language info. if (charset != null && i == 0) { retVal.Append(";\r\n\t" + parameter.Name + "*" + i.ToString() + "*=" + charset.WebName + "''" + values[i]); } else { retVal.Append(";\r\n\t" + parameter.Name + "*" + i.ToString() + "*=" + values[i]); } } } } return(retVal.ToString()); }
/// <summary> /// Returns MIME entity as byte[]. /// </summary> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <returns>Returns MIME entity as byte[].</returns> public byte[] ToByte(MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { return(ToByte(headerWordEncoder, headerParmetersCharset, false)); }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> internal protected override void ToStream(Stream stream,MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset,bool headerReencode) { if(stream == null){ throw new ArgumentNullException("stream"); } /* RFC 2046 5.1.1. NOTE: The CRLF preceding the boundary delimiter line is conceptually attached to the boundary so that it is possible to have a part that does not end with a CRLF (line break). dash-boundary := "--" boundary multipart-body := [preamble CRLF] dash-boundary transport-padding CRLF body-part *encapsulation close-delimiter transport-padding [CRLF epilogue] encapsulation := delimiter transport-padding CRLF body-part delimiter := CRLF dash-boundary close-delimiter := delimiter "--" */ // Set "preamble" text if any. if(!string.IsNullOrEmpty(m_TextPreamble)){ if(m_TextPreamble.EndsWith("\r\n")){ byte[] preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble); stream.Write(preableBytes,0,preableBytes.Length); } else{ byte[] preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble + "\r\n"); stream.Write(preableBytes,0,preableBytes.Length); } } for(int i=0;i<m_pBodyParts.Count;i++){ MIME_Entity bodyPart = m_pBodyParts[i]; // First boundary has no preceeding CRLF. if(i == 0){ byte[] bStart = Encoding.UTF8.GetBytes("--" + this.Entity.ContentType.Param_Boundary + "\r\n"); stream.Write(bStart,0,bStart.Length); } // Boundaries > 1. else{ byte[] bStart = Encoding.UTF8.GetBytes("\r\n--" + this.Entity.ContentType.Param_Boundary + "\r\n"); stream.Write(bStart,0,bStart.Length); } bodyPart.ToStream(stream,headerWordEncoder,headerParmetersCharset,headerReencode); // Last body part, close boundary. if(i == (m_pBodyParts.Count - 1)){ byte[] bEnd = Encoding.UTF8.GetBytes("\r\n--" + this.Entity.ContentType.Param_Boundary + "--"); stream.Write(bEnd,0,bEnd.Length); } } // Set "epilogoue" text if any. if(!string.IsNullOrEmpty(m_TextEpilogue)){ if(m_TextEpilogue.StartsWith("\r\n")){ byte[] epilogoueBytes = Encoding.UTF8.GetBytes(m_TextEpilogue); stream.Write(epilogoueBytes,0,epilogoueBytes.Length); } else{ byte[] epilogoueBytes = Encoding.UTF8.GetBytes("\r\n" + m_TextEpilogue); stream.Write(epilogoueBytes,0,epilogoueBytes.Length); } } }
/// <summary> /// Parses parameters from the specified reader. /// </summary> /// <param name="reader">MIME reader.</param> /// <exception cref="ArgumentNullException">Is raised when <b>reader</b> is null reference.</exception> public void Parse(MIME_Reader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } /* RFC 2231. */ while (true) { // End os stream reached. if (reader.Peek(true) == -1) { break; } // Next parameter start, just eat that char. else if (reader.Peek(true) == ';') { reader.Char(false); } else { string name = reader.Token(); string value = ""; // Parameter value specified. if (reader.Peek(true) == '=') { reader.Char(false); string v = reader.Word(); // Normally value may not be null, but following case: paramName=EOS. if (v != null) { value = v; } } // RFC 2231 encoded/splitted parameter. if (name.IndexOf('*') > -1) { string[] name_x_no_x = name.Split('*'); name = name_x_no_x[0]; Encoding charset = Encoding.ASCII; StringBuilder valueBuffer = new StringBuilder(); // We must have charset'language'value. // Examples: // URL*=utf-8''test; // URL*0*=utf-8''"test"; if ((name_x_no_x.Length == 2 && name_x_no_x[1] == "") || name_x_no_x.Length == 3) { string[] charset_language_value = value.Split('\''); charset = Encoding.GetEncoding(charset_language_value[0]); valueBuffer.Append(DecodeExtOctet(charset_language_value[2], charset)); } // No encoding, probably just splitted ASCII value. // Example: // URL*0="value1"; // URL*1="value2"; else { valueBuffer.Append(value); } // Read while value continues. while (true) { // End os stream reached. if (reader.Peek(true) == -1) { break; } // Next parameter start, just eat that char. else if (reader.Peek(true) == ';') { reader.Char(false); } else { if (!reader.StartsWith(name + "*")) { break; } reader.Token(); // Parameter value specified. if (reader.Peek(true) == '=') { reader.Char(false); string v = reader.Word(); // Normally value may not be null, but following case: paramName=EOS. if (v != null) { valueBuffer.Append(DecodeExtOctet(v, charset)); } } } } this[name] = valueBuffer.ToString(); } // Regular parameter. else { this[name] = MIME_Encoding_EncodedWord.DecodeS(value); } } } m_IsModified = false; }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> internal protected override void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset, bool headerReencode) { if (stream == null) { throw new ArgumentNullException("stream"); } /* RFC 2046 5.1.1. * NOTE: The CRLF preceding the boundary delimiter line is conceptually * attached to the boundary so that it is possible to have a part that * does not end with a CRLF (line break). * * dash-boundary := "--" boundary * * multipart-body := [preamble CRLF] * dash-boundary transport-padding CRLF * body-part *encapsulation * close-delimiter transport-padding * [CRLF epilogue] * * encapsulation := delimiter transport-padding * CRLF body-part * * delimiter := CRLF dash-boundary * * close-delimiter := delimiter "--" */ // Set "preamble" text if any. if (!string.IsNullOrEmpty(m_TextPreamble)) { if (m_TextPreamble.EndsWith("\r\n")) { byte[] preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble); stream.Write(preableBytes, 0, preableBytes.Length); } else { byte[] preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble + "\r\n"); stream.Write(preableBytes, 0, preableBytes.Length); } } for (int i = 0; i < m_pBodyParts.Count; i++) { MIME_Entity bodyPart = m_pBodyParts[i]; // First boundary has no preceeding CRLF. if (i == 0) { byte[] bStart = Encoding.UTF8.GetBytes("--" + this.Entity.ContentType.Param_Boundary + "\r\n"); stream.Write(bStart, 0, bStart.Length); } // Boundaries > 1. else { byte[] bStart = Encoding.UTF8.GetBytes("\r\n--" + this.Entity.ContentType.Param_Boundary + "\r\n"); stream.Write(bStart, 0, bStart.Length); } bodyPart.ToStream(stream, headerWordEncoder, headerParmetersCharset, headerReencode); // Last body part, close boundary. if (i == (m_pBodyParts.Count - 1)) { byte[] bEnd = Encoding.UTF8.GetBytes("\r\n--" + this.Entity.ContentType.Param_Boundary + "--"); stream.Write(bEnd, 0, bEnd.Length); } } // Set "epilogoue" text if any. if (!string.IsNullOrEmpty(m_TextEpilogue)) { if (m_TextEpilogue.StartsWith("\r\n")) { byte[] epilogoueBytes = Encoding.UTF8.GetBytes(m_TextEpilogue); stream.Write(epilogoueBytes, 0, epilogoueBytes.Length); } else { byte[] epilogoueBytes = Encoding.UTF8.GetBytes("\r\n" + m_TextEpilogue); stream.Write(epilogoueBytes, 0, epilogoueBytes.Length); } } }
/// <summary> /// Store MIME enity to the specified stream. /// </summary> /// <param name="stream">Stream where to store MIME entity. Storing starts form stream current position.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null.</exception> public void ToStream(Stream stream,MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset,bool headerReencode) { if(stream == null){ throw new ArgumentNullException("stream"); } m_pHeader.ToStream(stream,headerWordEncoder,headerParmetersCharset,headerReencode); stream.Write(new byte[]{(int)'\r',(int)'\n'},0,2); m_pBody.ToStream(stream,headerWordEncoder,headerParmetersCharset,headerReencode); }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder,Encoding parmetersCharset) { if(this.IsModified){ StringBuilder retVal = new StringBuilder(); retVal.Append(this.Name + ": "); for(int i=0;i<m_pAddresses.Count;i++){ if(i > 0){ retVal.Append("\t"); } // Don't add ',' for last item. if(i == (m_pAddresses.Count - 1)){ retVal.Append(m_pAddresses[i].ToString(wordEncoder) + "\r\n"); } else{ retVal.Append(m_pAddresses[i].ToString(wordEncoder) + ",\r\n"); } } return retVal.ToString(); } else{ return m_ParseValue; } }
/// <summary> /// Returns MIME entity as string. /// </summary> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <returns>Returns MIME entity as string.</returns> public string ToString(MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset,bool headerReencode) { using(MemoryStream ms = new MemoryStream()){ ToStream(ms,headerWordEncoder,headerParmetersCharset,headerReencode); return Encoding.UTF8.GetString(ms.ToArray()); } }
/// <summary> /// Constructs specified entity and it's childentities bodystructure string. /// </summary> /// <param name="entity">Mime entity.</param> /// <param name="bodystructure">Specifies if to construct BODY or BODYSTRUCTURE.</param> /// <returns></returns> private string ConstructParts(MIME_Entity entity,bool bodystructure) { /* RFC 3501 7.4.2 BODYSTRUCTURE BODY A form of BODYSTRUCTURE without extension data. A parenthesized list that describes the [MIME-IMB] body structure of a message. This is computed by the server by parsing the [MIME-IMB] header fields, defaulting various fields as necessary. For example, a simple text message of 48 lines and 2279 octets can have a body structure of: ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 2279 48) Multiple parts are indicated by parenthesis nesting. Instead of a body type as the first element of the parenthesized list, there is a sequence of one or more nested body structures. The second element of the parenthesized list is the multipart subtype (mixed, digest, parallel, alternative, etc.). For example, a two part message consisting of a text and a BASE64-encoded text attachment can have a body structure of: (("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 1152 23)("TEXT" "PLAIN" ("CHARSET" "US-ASCII" "NAME" "cc.diff") "<*****@*****.**>" "Compiler diff" "BASE64" 4554 73) "MIXED") Extension data follows the multipart subtype. Extension data is never returned with the BODY fetch, but can be returned with a BODYSTRUCTURE fetch. Extension data, if present, MUST be in the defined order. The extension data of a multipart body part are in the following order: body parameter parenthesized list A parenthesized list of attribute/value pairs [e.g., ("foo" "bar" "baz" "rag") where "bar" is the value of "foo", and "rag" is the value of "baz"] as defined in [MIME-IMB]. body disposition A parenthesized list, consisting of a disposition type string, followed by a parenthesized list of disposition attribute/value pairs as defined in [DISPOSITION]. body language A string or parenthesized list giving the body language value as defined in [LANGUAGE-TAGS]. body location A string list giving the body content URI as defined in [LOCATION]. Any following extension data are not yet defined in this version of the protocol. Such extension data can consist of zero or more NILs, strings, numbers, or potentially nested parenthesized lists of such data. Client implementations that do a BODYSTRUCTURE fetch MUST be prepared to accept such extension data. Server implementations MUST NOT send such extension data until it has been defined by a revision of this protocol. The basic fields of a non-multipart body part are in the following order: body type A string giving the content media type name as defined in [MIME-IMB]. body subtype A string giving the content subtype name as defined in [MIME-IMB]. body parameter parenthesized list A parenthesized list of attribute/value pairs [e.g., ("foo" "bar" "baz" "rag") where "bar" is the value of "foo" and "rag" is the value of "baz"] as defined in [MIME-IMB]. body id A string giving the content id as defined in [MIME-IMB]. body description A string giving the content description as defined in [MIME-IMB]. body encoding A string giving the content transfer encoding as defined in [MIME-IMB]. body size A number giving the size of the body in octets. Note that this size is the size in its transfer encoding and not the resulting size after any decoding. A body type of type MESSAGE and subtype RFC822 contains, immediately after the basic fields, the envelope structure, body structure, and size in text lines of the encapsulated message. A body type of type TEXT contains, immediately after the basic fields, the size of the body in text lines. Note that this size is the size in its content transfer encoding and not the resulting size after any decoding. Extension data follows the basic fields and the type-specific fields listed above. Extension data is never returned with the BODY fetch, but can be returned with a BODYSTRUCTURE fetch. Extension data, if present, MUST be in the defined order. The extension data of a non-multipart body part are in the following order: body MD5 A string giving the body MD5 value as defined in [MD5]. body disposition A parenthesized list with the same content and function as the body disposition for a multipart body part. body language A string or parenthesized list giving the body language value as defined in [LANGUAGE-TAGS]. body location A string list giving the body content URI as defined in [LOCATION]. Any following extension data are not yet defined in this version of the protocol, and would be as described above under multipart extension data. // We don't construct extention fields like rfc says: Server implementations MUST NOT send such extension data until it has been defined by a revision of this protocol. contentTypeMainMediaType - Example: 'TEXT' contentTypeSubMediaType - Example: 'PLAIN' conentTypeParameters - Example: '("CHARSET" "iso-8859-1" ...)' contentID - Content-ID: header field value. contentDescription - Content-Description: header field value. contentEncoding - Content-Transfer-Encoding: header field value. contentSize - mimeEntity ENCODED data size [envelope] - NOTE: included only if contentType = "message" !!! [contentLines] - number of ENCODED data lines. NOTE: included only if contentType = "text" !!! // Basic fields for multipart (nestedMimeEntries) contentTypeSubMediaType // Basic fields for non-multipart contentTypeMainMediaType contentTypeSubMediaType (conentTypeParameters) contentID contentDescription contentEncoding contentSize [envelope] [contentLine] */ MIME_Encoding_EncodedWord wordEncoder = new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.B,Encoding.UTF8); wordEncoder.Split = false; StringBuilder retVal = new StringBuilder(); // Multipart message if(entity.Body is MIME_b_Multipart){ retVal.Append("("); // Construct child entities. foreach(MIME_Entity childEntity in ((MIME_b_Multipart)entity.Body).BodyParts){ // Construct child entity. This can be multipart or non multipart. retVal.Append(ConstructParts(childEntity,bodystructure)); } // Add contentTypeSubMediaType if(entity.ContentType != null && entity.ContentType.SubType != null){ retVal.Append(" \"" + entity.ContentType.SubType + "\""); } else{ retVal.Append(" NIL"); } retVal.Append(")"); } // Single part message else{ retVal.Append("("); // NOTE: all header fields and parameters must in ENCODED form !!! // Add contentTypeMainMediaType if(entity.ContentType != null && entity.ContentType.Type != null){ retVal.Append("\"" + entity.ContentType.Type + "\""); } else{ retVal.Append("NIL"); } // Add contentTypeSubMediaType if(entity.ContentType != null && entity.ContentType.SubType != null){ retVal.Append(" \"" + entity.ContentType.SubType + "\""); } else{ retVal.Append(" NIL"); } // conentTypeParameters - Syntax: {("name" SP "value" *(SP "name" SP "value"))} if(entity.ContentType != null){ if(entity.ContentType.Parameters.Count > 0){ retVal.Append(" ("); bool first = true; foreach(MIME_h_Parameter parameter in entity.ContentType.Parameters){ // For the first item, don't add SP. if(first){ first = false; } else{ retVal.Append(" "); } retVal.Append("\"" + parameter.Name + "\" \"" + wordEncoder.Encode(parameter.Value) + "\""); } retVal.Append(")"); } else{ retVal.Append(" NIL"); } } else{ retVal.Append(" NIL"); } // contentID string contentID = entity.ContentID; if(contentID != null){ retVal.Append(" \"" + wordEncoder.Encode(contentID) + "\""); } else{ retVal.Append(" NIL"); } // contentDescription string contentDescription = entity.ContentDescription; if(contentDescription != null){ retVal.Append(" \"" + wordEncoder.Encode(contentDescription) + "\""); } else{ retVal.Append(" NIL"); } // contentEncoding if(entity.ContentTransferEncoding != null){ retVal.Append(" \"" + wordEncoder.Encode(entity.ContentTransferEncoding) + "\""); } else{ // If not specified, then must be 7bit. retVal.Append(" \"7bit\""); } // contentSize if(entity.Body is MIME_b_SinglepartBase){ retVal.Append(" " + ((MIME_b_SinglepartBase)entity.Body).EncodedData.Length.ToString()); } else{ retVal.Append(" 0"); } // envelope ---> FOR ContentType: message/rfc822 ONLY ### if(entity.Body is MIME_b_MessageRfc822){ retVal.Append(" " + IMAP_t_Fetch_r_i_Envelope.ConstructEnvelope(((MIME_b_MessageRfc822)entity.Body).Message)); // TODO: BODYSTRUCTURE,LINES } // contentLines ---> FOR ContentType: text/xxx ONLY ### if(entity.Body is MIME_b_Text){ long lineCount = 0; StreamLineReader r = new StreamLineReader(new MemoryStream(((MIME_b_SinglepartBase)entity.Body).EncodedData)); byte[] line = r.ReadLine(); while(line != null){ lineCount++; line = r.ReadLine(); } retVal.Append(" " + lineCount.ToString()); } retVal.Append(")"); } return retVal.ToString(); }
/// <summary> /// Returns MIME entity as byte[]. /// </summary> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <returns>Returns MIME entity as byte[].</returns> public byte[] ToByte(MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset,bool headerReencode) { using(MemoryStream ms = new MemoryStream()){ ToStream(ms,headerWordEncoder,headerParmetersCharset,headerReencode); return ms.ToArray(); } }
/// <summary> /// Construct secified mime entity ENVELOPE string. /// </summary> /// <param name="entity">Mail message.</param> /// <returns></returns> public static string ConstructEnvelope(Mail_Message entity) { /* RFC 3501 7.4.2 ENVELOPE A parenthesized list that describes the envelope structure of a message. This is computed by the server by parsing the [RFC-2822] header into the component parts, defaulting various fields as necessary. The fields of the envelope structure are in the following order: date, subject, from, sender, reply-to, to, cc, bcc, in-reply-to, and message-id. The date, subject, in-reply-to, and message-id fields are strings. The from, sender, reply-to, to, cc, and bcc fields are parenthesized lists of address structures. An address structure is a parenthesized list that describes an electronic mail address. The fields of an address structure are in the following order: personal name, [SMTP] at-domain-list (source route), mailbox name, and host name. [RFC-2822] group syntax is indicated by a special form of address structure in which the host name field is NIL. If the mailbox name field is also NIL, this is an end of group marker (semi-colon in RFC 822 syntax). If the mailbox name field is non-NIL, this is a start of group marker, and the mailbox name field holds the group name phrase. If the Date, Subject, In-Reply-To, and Message-ID header lines are absent in the [RFC-2822] header, the corresponding member of the envelope is NIL; if these header lines are present but empty the corresponding member of the envelope is the empty string. Note: some servers may return a NIL envelope member in the "present but empty" case. Clients SHOULD treat NIL and empty string as identical. Note: [RFC-2822] requires that all messages have a valid Date header. Therefore, the date member in the envelope can not be NIL or the empty string. Note: [RFC-2822] requires that the In-Reply-To and Message-ID headers, if present, have non-empty content. Therefore, the in-reply-to and message-id members in the envelope can not be the empty string. If the From, To, cc, and bcc header lines are absent in the [RFC-2822] header, or are present but empty, the corresponding member of the envelope is NIL. If the Sender or Reply-To lines are absent in the [RFC-2822] header, or are present but empty, the server sets the corresponding member of the envelope to be the same value as the from member (the client is not expected to know to do this). Note: [RFC-2822] requires that all messages have a valid From header. Therefore, the from, sender, and reply-to members in the envelope can not be NIL. ENVELOPE ("date" "subject" from sender reply-to to cc bcc "in-reply-to" "messageID") */ // NOTE: all header fields and parameters must in ENCODED form !!! MIME_Encoding_EncodedWord wordEncoder = new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.B,Encoding.UTF8); wordEncoder.Split = false; StringBuilder retVal = new StringBuilder(); retVal.Append("ENVELOPE ("); // date try{ if(entity.Date != DateTime.MinValue){ retVal.Append(TextUtils.QuoteString(MIME_Utils.DateTimeToRfc2822(entity.Date))); } else{ retVal.Append("NIL"); } } catch{ retVal.Append("NIL"); } // subject if(entity.Subject != null){ //retVal.Append(" " + TextUtils.QuoteString(wordEncoder.Encode(entity.Subject))); string val = wordEncoder.Encode(entity.Subject); retVal.Append(" {" + val.Length + "}\r\n" + val); } else{ retVal.Append(" NIL"); } // from if(entity.From != null && entity.From.Count > 0){ retVal.Append(" " + ConstructAddresses(entity.From.ToArray(),wordEncoder)); } else{ retVal.Append(" NIL"); } // sender // NOTE: There is confusing part, according rfc 2822 Sender: is MailboxAddress and not AddressList. if(entity.Sender != null){ retVal.Append(" ("); retVal.Append(ConstructAddress(entity.Sender,wordEncoder)); retVal.Append(")"); } else{ retVal.Append(" NIL"); } // reply-to if(entity.ReplyTo != null){ retVal.Append(" " + ConstructAddresses(entity.ReplyTo.Mailboxes,wordEncoder)); } else{ retVal.Append(" NIL"); } // to if(entity.To != null && entity.To.Count > 0){ retVal.Append(" " + ConstructAddresses(entity.To.Mailboxes,wordEncoder)); } else{ retVal.Append(" NIL"); } // cc if(entity.Cc != null && entity.Cc.Count > 0){ retVal.Append(" " + ConstructAddresses(entity.Cc.Mailboxes,wordEncoder)); } else{ retVal.Append(" NIL"); } // bcc if(entity.Bcc != null && entity.Bcc.Count > 0){ retVal.Append(" " + ConstructAddresses(entity.Bcc.Mailboxes,wordEncoder)); } else{ retVal.Append(" NIL"); } // in-reply-to if(entity.InReplyTo != null){ retVal.Append(" " + TextUtils.QuoteString(wordEncoder.Encode(entity.InReplyTo))); } else{ retVal.Append(" NIL"); } // message-id if(entity.MessageID != null){ retVal.Append(" " + TextUtils.QuoteString(wordEncoder.Encode(entity.MessageID))); } else{ retVal.Append(" NIL"); } retVal.Append(")"); return retVal.ToString(); }
/// <summary> /// Stores header to the specified stream. /// </summary> /// <param name="stream">Stream where to store header.</param> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> public void ToStream(Stream stream, MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { ToStream(stream, wordEncoder, parmetersCharset, false); }
/// <summary> /// Returns header field value as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded. /// If encoding needed, UTF-8 is strongly reccomended if not sure.</param> /// <returns>Returns header field value as string.</returns> public string ValueToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { return(ToString(wordEncoder, parmetersCharset).Split(new char[] { ':' }, 2)[1].TrimStart()); }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> protected internal override void ToStream(Stream stream,MIME_Encoding_EncodedWord headerWordEncoder,Encoding headerParmetersCharset,bool headerReencode) { if(stream == null){ throw new ArgumentNullException("stream"); } /* RFC 2046 5.1.1. NOTE: The CRLF preceding the boundary delimiter line is conceptually attached to the boundary so that it is possible to have a part that does not end with a CRLF (line break). */ // Set "preamble" text if any. if(!string.IsNullOrEmpty(m_TextPreamble)){ byte[] preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble); stream.Write(preableBytes,0,preableBytes.Length); } for(int i=0;i<m_pBodyParts.Count;i++){ MIME_Entity bodyPart = m_pBodyParts[i]; // Start new body part. byte[] bStart = Encoding.UTF8.GetBytes("\r\n--" + this.Entity.ContentType.Param_Boundary + "\r\n"); stream.Write(bStart,0,bStart.Length); bodyPart.ToStream(stream,headerWordEncoder,headerParmetersCharset,headerReencode); // Last body part, close boundary. if(i == (m_pBodyParts.Count - 1)){ byte[] bEnd = Encoding.UTF8.GetBytes("\r\n--" + this.Entity.ContentType.Param_Boundary + "--\r\n"); stream.Write(bEnd,0,bEnd.Length); } } // Set "epilogoue" text if any. if(!string.IsNullOrEmpty(m_TextEpilogue)){ byte[] epilogoueBytes = Encoding.UTF8.GetBytes(m_TextEpilogue); stream.Write(epilogoueBytes,0,epilogoueBytes.Length); } }
/// <summary> /// Returns MIME header as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <returns>Returns MIME header as string.</returns> public string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { return(ToString(wordEncoder, parmetersCharset, false)); }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <param name="reEncode">If true always specified encoding is used. If false and header field value not modified, original encoding is kept.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder,Encoding parmetersCharset,bool reEncode) { if(!reEncode && m_ParseValue != null){ return m_ParseValue; } else{ return m_Name + ": " + m_pAddress.ToString(wordEncoder) + "\r\n"; } }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <param name="reEncode">If true always specified encoding is used. If false and header field value not modified, original encoding is kept.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder,Encoding parmetersCharset,bool reEncode) { if(!reEncode && !this.IsModified){ return m_ParseValue; } else{ StringBuilder retVal = new StringBuilder(); retVal.Append("Content-Type: " + m_Type + "/" + m_SubType); retVal.Append(m_pParameters.ToString(parmetersCharset)); retVal.Append("\r\n"); return retVal.ToString(); } }