//load all mime part from a string buffer public virtual void LoadBody(string strData) { if (strData == null) { throw new ArgumentNullException(); } int headend = strData.IndexOf("\r\n\r\n"); LoadHead(strData.Substring(0, headend + 2)); int bodystart = headend + 4; if (MimeType.MediaType.MEDIA_MULTIPART == GetMediaType()) { string boundary = GetBoundary(); if (null == boundary) { return; } else { string strBstart = "--" + boundary; string strBend = strBstart + "--"; int nBstart = strData.IndexOf(strBstart, bodystart); if (nBstart == -1) { return; } int nBend = strData.IndexOf(strBend, bodystart); if (nBend == -1) { nBend = strData.Length; } if (nBstart > bodystart) { mContent = strData.Substring(bodystart, nBstart - bodystart); } while (nBstart < nBend) { nBstart = nBstart + strBstart.Length + 2; int nBstart2 = strData.IndexOf(strBstart, nBstart); if (nBstart2 == -1) { nBstart2 = nBend; } MimeBody ChildBody = CreatePart(); ChildBody.LoadBody(strData.Substring(nBstart, nBstart2 - nBstart)); nBstart = nBstart2; } } } else { mContent = strData.Substring(bodystart, strData.Length - bodystart); } }
//create a child mime part public MimeBody CreatePart(MimeBody parent) { MimeBody aMimeBody = new MimeBody(); if (parent != null) { int index = ChildList.IndexOf(parent); if (index != -1) { ChildList.Insert(index + 1, aMimeBody); return(aMimeBody); } } ChildList.Add(aMimeBody); return(aMimeBody); }
// get a list of mime part public void GetBodyPartList(ArrayList BodyList) { if (BodyList == null) { throw new ArgumentNullException(); } if (GetMediaType() != MimeType.MediaType.MEDIA_MULTIPART) { BodyList.Add(this); } else { BodyList.Add(this); for (int i = 0; i < ChildList.Count; i++) { MimeBody aMimeBody = (MimeBody)ChildList[i]; aMimeBody.GetBodyPartList(BodyList); } } }
//store all mime part to a string buffer public void StoreBody(StringBuilder sb) { if (sb == null) { throw new ArgumentNullException(); } StoreHead(sb); sb.Append(mContent); if (MimeType.MediaType.MEDIA_MULTIPART == GetMediaType()) { string boundary = GetBoundary(); for (int i = 0; i < ChildList.Count; i++) { sb.AppendFormat("--{0}\r\n", boundary); MimeBody aMimeBody = (MimeBody)ChildList[i]; aMimeBody.StoreBody(sb); } sb.AppendFormat("--{0}--\r\n", boundary); } }
public void TestMimeMessage() { string[] tests = { MimeMessages.SimpleNDR, MimeMessages.BinaryNDR, MimeMessages.BrokenNDR }; /// http://www.codeproject.com/useritems/MIME_De_Encode_in_C_.asp foreach (string test in tests) { MimeMessage msg = new MimeMessage(); msg.LoadBody(test); ArrayList bodylist = new ArrayList(); msg.GetBodyPartList(bodylist); for (int i = 0; i < bodylist.Count; i++) { MIME.MimeBody ab = (MimeBody)bodylist[i]; Console.WriteLine(ab.GetType().Name); Console.WriteLine(" {0}", ab.GetContentType()); switch (ab.GetContentType()) { case "message/delivery-status": /// TODO: move to Mime processor MimeDSN dsn = new MimeDSN(); dsn.LoadBody(ab.GetText()); Console.WriteLine("ReportingMTA: {0}", dsn.ReportingMTA); Console.WriteLine("ReceivedFromMTA: {0}", dsn.ReceivedFromMTA); Console.WriteLine("OriginalEnvelopeId: {0}", dsn.OriginalEnvelopeId); foreach (MimeDSNRecipient r in dsn.Recipients) { Console.WriteLine("{0}: {1}", r.Action, r.FinalRecipientEmailAddress); } break; } } } }
public void TestEmbeddedResources() { Assembly assembly = Assembly.GetExecutingAssembly(); string[] resources = assembly.GetManifestResourceNames(); /// http://www.codeproject.com/useritems/MIME_De_Encode_in_C_.asp foreach (string resource in resources) { bool expected_found = false; if (resource.StartsWith("MIME.NET.Tests.Tests._")) { expected_found = true; } else if (resource.StartsWith("MIME.NET.Tests.Failures._")) { expected_found = false; } else { continue; } Console.WriteLine(resource); StreamReader sr = new StreamReader(assembly.GetManifestResourceStream(resource)); string filename = Path.GetTempFileName(); StreamWriter sw = new StreamWriter(filename); sw.Write(sr.ReadToEnd()); sw.Close(); MimeMessage msg = new MimeMessage(); StreamReader content = new StreamReader(filename); msg.LoadBody(content.ReadToEnd()); content.Close(); bool found = false; ArrayList bodylist = new ArrayList(); msg.GetBodyPartList(bodylist); for (int i = 0; i < bodylist.Count; i++) { MIME.MimeBody ab = (MimeBody)bodylist[i]; Console.WriteLine(ab.GetType().Name); Console.WriteLine(" {0}", ab.GetContentType()); switch (ab.GetContentType()) { case "message/delivery-status": /// TODO: move to Mime processor MimeDSN dsn = new MimeDSN(); dsn.LoadBody(ab.GetText()); Console.WriteLine("ReportingMTA: {0}", dsn.ReportingMTA); Console.WriteLine("ReceivedFromMTA: {0}", dsn.ReceivedFromMTA); Console.WriteLine("OriginalEnvelopeId: {0}", dsn.OriginalEnvelopeId); foreach (MimeDSNRecipient r in dsn.Recipients) { found = true; Console.WriteLine("{0}: {1}", r.Action, r.FinalRecipientEmailAddress); } break; } } File.Delete(filename); Console.WriteLine("{0}: {1}", resource, found); Console.WriteLine("-------------------------------------------------------------------------"); Assert.AreEqual(expected_found, found); } }
//create a child mime part public MimeBody CreatePart(MimeBody parent) { MimeBody aMimeBody = new MimeBody(); if(parent != null) { int index = ChildList.IndexOf(parent); if(index != -1) { ChildList.Insert(index+1, aMimeBody); return aMimeBody; } } ChildList.Add(aMimeBody); return aMimeBody; }
public void ErasePart(MimeBody ChildPart) { ChildList.Remove(ChildPart); }