public MIMEBody(string Input,MIMEHeader Header) { if (string.IsNullOrEmpty(Input)) throw new ArgumentNullException("Input"); this.Boundries = new List<MIMEMessage>(); MediaEnum ContentType; ContentType=GetMediaType(Header); if(MediaEnum.MEDIA_MULTIPART==ContentType) { string CurrentBoundry=GetBoundryMarker(Header); if (string.IsNullOrEmpty(CurrentBoundry)) return; CurrentBoundry = CurrentBoundry.Replace("\"", ""); string BoundryStart = "--"+CurrentBoundry; string BoundryEnd = BoundryStart+"--"; int StartIndex = Input.IndexOf(BoundryStart, 0, StringComparison.InvariantCulture); if (StartIndex == -1) return; int EndIndex = Input.IndexOf(BoundryEnd, 0, StringComparison.InvariantCulture); if (EndIndex == -1) EndIndex = Input.Length; Content = Input.Substring(0, StartIndex); while (StartIndex < EndIndex) { StartIndex += BoundryStart.Length + 2; if (StartIndex >= EndIndex) break; int TempIndex = Input.IndexOf(BoundryStart, StartIndex, StringComparison.InvariantCulture); if (TempIndex != -1) { Boundries.Add(new MIMEMessage(Input.Substring(StartIndex, TempIndex - StartIndex))); } else break; StartIndex = TempIndex; } } else { Content = Input; } string Encoding = ""; try { Encoding = Header[Constants.TransferEncoding].Attributes[0].Value; } catch { Encoding = Constants.Encoding7Bit; } Code CodeUsing = CodeManager.Instance[Encoding]; CodeUsing.CharacterSet = Header[Constants.ContentType][Constants.Charset]; string TempContent = ""; CodeUsing.Decode(Content, out TempContent); Content = TempContent; }
private static MediaEnum GetMediaType(MIMEHeader Header) { string ContentType = GetContentType(Header); int x=0; foreach (string TempType in MIMEType.TypeTable) { if (TempType.Equals(ContentType, StringComparison.InvariantCultureIgnoreCase)) { return (MediaEnum)x; } ++x; } return (MediaEnum)MIMEType.TypeTable.Length - 1; }
/// <summary> /// Gets the boundary marker /// </summary> /// <param name="Header">Header of the message</param> /// <returns>A string containing the boundary marker</returns> private static string GetBoundryMarker(MIMEHeader Header) { return Header[Constants.ContentType][Constants.Boundary]; }
/// <summary> /// Gets the content type /// </summary> /// <param name="Header">Header of the message</param> /// <returns>A string containing the content type</returns> private static string GetContentType(MIMEHeader Header) { if (Header != null && Header[Constants.ContentType] != null && Header[Constants.ContentType].Attributes.Count > 0) { string ContentType = Header[Constants.ContentType].Attributes[0].Value; if (null != ContentType) { int Index = ContentType.IndexOf('/', 0); if (Index != -1) { return ContentType.Substring(0, Index); } else { return ContentType; } } } return "text"; }
public void LoadMessage(string Input) { _Content = Input; int HeaderEnd = Input.IndexOf("\r\n\r\n", StringComparison.InvariantCulture); Header = new MIMEHeader(Input.Substring(0, HeaderEnd + 2)); Input = Input.Substring(HeaderEnd + 2); Body = new MIMEBody(Input, Header); }