// Метод загрузки текста public static MyMailMessage LoadBody(MailBox box) { MyMailMessage outMess = new MyMailMessage(box.SelectedFolder.Message); XmlElement folders = null; XmlElement messages = null; if (!File.Exists(filename)) { CreateXML(filename); } XmlDocument xd = new XmlDocument(); xd.Load(filename); XmlNodeList list = xd.DocumentElement.ChildNodes; // Выбор почтового ящика по адресу foreach (XmlNode node in list) { if (node.Attributes["address"].Value == box.MyAddress.Address) { folders = (XmlElement)node.LastChild; XmlNodeList fList = folders.ChildNodes; // Выбор папки по указателю foreach (XmlNode fld in fList) { if (fld.Attributes["pointer"].Value == box.SelectedFolder.Pointer) { messages = (XmlElement)fld.FirstChild; XmlNodeList messList = messages.ChildNodes; // Выбор письма по идентификатору foreach (XmlElement message in messList) { if (message.Attributes["id"].Value == box.SelectedFolder.Message.ID) { outMess.Body = message.GetAttribute("body"); string isHtml = message.GetAttribute("isHtml") == "" ? "False" : message.GetAttribute("isHtml"); outMess.IsBodyHtml = Convert.ToBoolean(isHtml); XmlElement attachments = (XmlElement)message.LastChild; XmlNodeList attachList = attachments.ChildNodes; foreach (XmlElement attachment in attachList) { ContentType ct = new ContentType(attachment.GetAttribute("contenttype")); outMess.MyAttachments.Add(new MyAttachment(ct, null)); } break; } } break; } } break; } } return(outMess); }
// Метод добавления тела письма из файла public void AddBody(MailBox box) { MyMailMessage mess = MailStorage.LoadBody(box); Body = mess.Body; IsBodyHtml = mess.IsBodyHtml; myAttachments = new List <MyAttachment>(); foreach (MyAttachment at in mess.myAttachments) { myAttachments.Add(new MyAttachment(at)); } }
// Создание копии письма public MyMailMessage(MyMailMessage message) { Headers.Add("main", message.Headers.GetValues("main")[0]); num = message.Num; id = message.ID; Sender = From = message.From; foreach (MailAddress adr in message.To) { To.Add(adr); } Subject = message.Subject; foreach (MailAddress adr in message.ReplyToList) { ReplyToList.Add(adr); } foreach (MailAddress adr in message.CC) { CC.Add(adr); } foreach (MailAddress adr in message.Bcc) { Bcc.Add(adr); } date = message.Date; displayDate = message.DisplayDate; mainContentType = message.mainContentType; BodyEncoding = message.BodyEncoding; BodyTransferEncoding = message.BodyTransferEncoding; isHaveAttachments = message.IsHaveAttachments; IsBodyHtml = message.IsBodyHtml; myAttachments = new List <MyAttachment>(); foreach (MyAttachment att in message.MyAttachments) { myAttachments.Add(att); } Body = message.Body; }
// Метод сохранения текста загруженного письма с вложенными файлами public static void SaveMessage(MailBox box) { XmlElement folders = null; XmlElement messages = null; if (!File.Exists(filename)) { CreateXML(filename); } XmlDocument xd = new XmlDocument(); xd.Load(filename); XmlNodeList list = xd.DocumentElement.ChildNodes; // Выбор почтового ящика по адресу foreach (XmlNode node in list) { if (node.Attributes["address"].Value == box.MyAddress.Address) { folders = (XmlElement)node.LastChild; XmlNodeList fList = folders.ChildNodes; // Выбор папки по указателю foreach (XmlNode fld in fList) { if (fld.Attributes["pointer"].Value == box.SelectedFolder.Pointer) { messages = (XmlElement)fld.FirstChild; XmlNodeList messList = messages.ChildNodes; // Выбор письма по идентификатору foreach (XmlElement message in messList) { if (message.Attributes["id"].Value == box.SelectedFolder.Message.ID) { // Добавление тела письма MyMailMessage msg = box.SelectedFolder.Message; message.SetAttribute("body", msg.Body); message.SetAttribute("isHtml", msg.IsBodyHtml.ToString()); XmlElement attachments = (XmlElement)message.LastChild; XmlNodeList attachList = attachments.ChildNodes; if (attachList.Count == 0) { // Добавление вложений foreach (MyAttachment attach in msg.MyAttachments) { XmlElement attachment = xd.CreateElement("attachment"); attachment.SetAttribute("contenttype", attach.ContentTypeAt.ToString()); attachment.SetAttribute("content", attach.Content); attachment.SetAttribute("name", attach.Name); attachments.AppendChild(attachment); } } break; } } break; } } break; } } xd.Save(filename); }