/// <summary> /// Parses header field from the specified value. /// </summary> /// <param name="value">Header field value. Header field name must be included. For example: 'Return-Path: <[email protected]>'.</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 Mail_h_ReturnPath Parse(string value) { if(value == null){ throw new ArgumentNullException("value"); } string[] name_value = value.Split(new char[]{':'},2); if(name_value.Length != 2){ throw new ParseException("Invalid header field value '" + value + "'."); } Mail_h_ReturnPath retVal = new Mail_h_ReturnPath(null); MIME_Reader r = new MIME_Reader(name_value[1].Trim()); r.ToFirstChar(); // Return-Path missing <>, some server won't be honor RFC. if(!r.StartsWith("<")){ retVal.m_Address = r.ToEnd(); } else{ retVal.m_Address = r.ReadParenthesized(); } return retVal; }
/// <summary> /// Parses header field from the specified value. /// </summary> /// <param name="value">Header field value. Header field name must be included. For example: 'Sender: [email protected]'.</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 Mail_h_Received Parse(string value) { if(value == null){ throw new ArgumentNullException("value"); } string[] name_value = value.Split(new char[]{':'},2); if(name_value.Length != 2){ throw new ParseException("Invalid header field value '" + value + "'."); } Mail_h_Received retVal = new Mail_h_Received("a","b",DateTime.MinValue); MIME_Reader r = new MIME_Reader(name_value[1]); while(true){ string word = r.Word(); // We processed all data. if(word == null && r.Available == 0){ break; } // We have comment, just eat it. else if(r.StartsWith("(")){ r.ReadParenthesized(); } // We have date-time or unknown-data. else if(r.StartsWith(";")){ // Eat ';' r.Char(false); try{ retVal.m_Time = MIME_Utils.ParseRfc2822DateTime(r.QuotedReadToDelimiter(new char[]{';'})); } catch{ // We hane some unknown data, skip it. } } else{ // We have some unexpected char like: .,= ... . Just eat it. if(word == null){ r.Char(true); continue; } word = word.ToUpperInvariant(); if(word == "FROM"){ retVal.m_From = r.DotAtom(); r.ToFirstChar(); if(r.StartsWith("(")){ string[] parts = r.ReadParenthesized().Split(' '); if(parts.Length == 1){ if(Net_Utils.IsIPAddress(parts[0])){ retVal.m_pFrom_TcpInfo = new Mail_t_TcpInfo(IPAddress.Parse(parts[0]),null); } } else if(parts.Length == 2){ string ip = parts[1].Trim(); if (ip.StartsWith("[")) ip = ip.Substring(1); if (ip.EndsWith("]")) ip = ip.Substring(0, ip.Length - 1); if(Net_Utils.IsIPAddress(ip)){ retVal.m_pFrom_TcpInfo = new Mail_t_TcpInfo(IPAddress.Parse(ip),parts[0]); } } } } else if(word == "BY"){ retVal.m_By = r.DotAtom(); r.ToFirstChar(); if(r.StartsWith("(")){ string[] parts = r.ReadParenthesized().Split(' '); if(parts.Length == 1){ if(Net_Utils.IsIPAddress(parts[0])){ retVal.m_pBy_TcpInfo = new Mail_t_TcpInfo(IPAddress.Parse(parts[0]),null); } } else if(parts.Length == 2){ if(Net_Utils.IsIPAddress(parts[1])){ retVal.m_pBy_TcpInfo = new Mail_t_TcpInfo(IPAddress.Parse(parts[1]),parts[0]); } } } } else if(word == "VIA"){ retVal.m_Via = r.Word(); } else if(word == "WITH"){ retVal.m_With = r.Word(); } else if(word == "ID"){ // msg-id = [CFWS] "<" id-left "@" id-right ">" [CFWS] if(r.StartsWith("<")){ retVal.m_ID = r.ReadParenthesized(); } else{ retVal.m_ID = r.Atom(); } } else if(word == "FOR"){ r.ToFirstChar(); // path / angle-address if(r.StartsWith("<")){ retVal.m_For = r.ReadParenthesized(); } else{ string mailbox = Mail_Utils.SMTP_Mailbox(r); if(mailbox == null){ throw new ParseException("Invalid Received: For parameter value '" + r.ToEnd() + "'."); } retVal.m_For = mailbox; } } // Unknown, just eat value. else{ r.Word(); } } } retVal.m_ParseValue = value; return retVal; }