// https://stackoverflow.com/questions/24170852/strip-attachments-from-emails-using-mailkit-mimekit static void SaveAttachments() { MimeKit.MimeMessage mimeMessage = MimeKit.MimeMessage.Load(@"x:\sample.eml"); System.Collections.Generic.List <MimeKit.MimeEntity> attachments = System.Linq.Enumerable.ToList(mimeMessage.Attachments); if (System.Linq.Enumerable.Any(attachments)) { // Only multipart mails can have attachments MimeKit.Multipart multipart = mimeMessage.Body as MimeKit.Multipart; if (multipart != null) { foreach (MimeKit.MimeEntity attachment in mimeMessage.Attachments) { multipart.Remove(attachment); } // Next attachment } // End if (multipart != null) mimeMessage.Body = multipart; } // End if(System.Linq.Enumerable.Any(attachments)) mimeMessage.WriteTo(new System.IO.FileStream(@"x:\stripped.eml", System.IO.FileMode.CreateNew)); } // End Sub SaveAttachments
private void WriteEmailsToDirectory(List <string> lstEmailsSid, string folderToSync, uint policyKey) { ASCommandResponse commandResponse = null; int fileNo = 1; foreach (string sid in lstEmailsSid) { string nazwaPlikuZKatalogiem = mailDir + "\\" + sid.Replace(":", ""); if (!File.Exists(nazwaPlikuZKatalogiem + ".eml")) { string fetchXml = SetFetchDataObjectAsXml(folderToSync, sid, true); StringBuilder xmlBuilder = new StringBuilder(); xmlBuilder.Append(fetchXml); ASCommandRequest commandRequest = CreateCommandRequest("ItemOperations", cred, devID, devType, protVer, server, username, xmlBuilder.ToString(), policyKey); // Send the request commandResponse = commandRequest.GetResponse(); EAS.generated.ItemOperationsResponseNamespace.ItemOperations emailFetched = GetFetchedMail(commandResponse.XmlString); if (emailFetched != null && emailFetched.Response != null && emailFetched.Response.Fetch != null && emailFetched.Response.Fetch.Count() > 0) { //WYCHWYC BODY EAS.generated.ItemOperationsResponseNamespace.Body b = null; List <EAS.generated.ItemOperationsResponseNamespace.ItemsChoiceType3> lst = emailFetched.Response.Fetch[0].Properties.ItemsElementName.ToList(); int index = lst.FindIndex(q => q == EAS.generated.ItemOperationsResponseNamespace.ItemsChoiceType3.Body); if (index > 0) { if (emailFetched.Response.Fetch[0].Properties.ItemsElementName[index] == EAS.generated.ItemOperationsResponseNamespace.ItemsChoiceType3.Body) { b = (EAS.generated.ItemOperationsResponseNamespace.Body)emailFetched.Response.Fetch[0].Properties.Items[index]; } } if (b != null) { string s = b.Data; MemoryStream ms = new MemoryStream(); StreamWriter sw = new StreamWriter(ms); sw.Write(s); sw.Flush(); ms.Seek(0, SeekOrigin.Begin); MimeKit.MimeMessage mm = MimeKit.MimeMessage.Load(ms); sw.Close(); ms.Close(); if (!string.IsNullOrEmpty(mm.HtmlBody)) { string body = mm.HtmlBody; string sContent = BitConverter.ToString(Encoding.UTF8.GetBytes(body)); int idx = 0; do { idx = sContent.IndexOf("C3", idx); if (idx != -1) { string podciag = sContent.Substring(idx, sContent.Length - idx); if (podciag.StartsWith("C3-83") || podciag.StartsWith("C3-84") || podciag.StartsWith("C3-85") || podciag.StartsWith("C3-82") || podciag.StartsWith("C3-A2")) //WYCINA CUDZYSŁÓW i apostrof w pewnych dziwnych przypadkach.. { if (podciag.Length > 10) { //string subPodciag = podciag.Substring(9, 2); //if ( subPodciag != "AD" || subPodciag != "AA" || subPodciag != "AC" || subPodciag != "AE" || subPodciag != "AF" //|| subPodciag != "A1" || subPodciag != "A2" || subPodciag != "A0" || subPodciag != "A3" || subPodciag != "A4" || subPodciag != "A5" || subPodciag != "A6" || subPodciag != "A7" || subPodciag != "A8" || subPodciag != "A9" //|| subPodciag != "B0" || subPodciag != "B1" || subPodciag != "B2" || subPodciag != "B3" || subPodciag != "B4" || subPodciag != "B5" || subPodciag != "B6" || subPodciag != "B7" || subPodciag != "B8" || subPodciag != "B9" || subPodciag != "B0" //|| subPodciag != "BA" || subPodciag != "BB" || subPodciag != "BC" || subPodciag != "BD" || subPodciag != "BE" || subPodciag != "BF") { sContent = sContent.Remove(idx + 1, 3); sContent = sContent.Remove(idx + 3, 3); } } } idx++; } } while (idx != -1); string[] sArr = sContent.Split('-'); byte[] bNew = new byte[sArr.Length]; for (int i = 0; i < sArr.Length; i++) { byte byt = Convert.ToByte(sContent.Substring(3 * i, 2), 16); bNew[i] = byt; } string enc = Encoding.UTF8.GetString(bNew); s = s.Replace(body, enc); //recreate object ms = new MemoryStream(); sw = new StreamWriter(ms); sw.Write(s); sw.Flush(); ms.Seek(0, SeekOrigin.Begin); mm = MimeKit.MimeMessage.Load(ms); sw.Close(); ms.Close(); } mm.WriteTo(nazwaPlikuZKatalogiem + ".eml"); OnNewFilesNumberEvent(string.Format("{0} {1}", fileNo, _rm.GetString("strEmailsDownloaded"))); fileNo++; } } else { MessageBox.Show(_rm.GetString("errSerialization")); } } } }