using System; using System.Collections.Generic; using System.Net.Mail; using System.Text; using Neo.Afx.ComponentModel; namespace Neo.Afx.WinService.Messages { public static class Pop3Extensions { public static Pop3Message ToPop3Message(this Lesnikowski.Mail.SimpleMailMessage pop3Message) { var message = new Pop3Message { Bcc = pop3Message.Bcc.ToStringSafe(), CC = Extensions.ToStringSafe(pop3Message.Cc), From = Extensions.ToStringSafe(pop3Message.From), ReplyTo = Extensions.ToStringSafe(pop3Message.ReplyTo), Sender = Extensions.ToStringSafe(pop3Message.Sender), To = Extensions.ToStringSafe(pop3Message.To), Priority = pop3Message.Importance.ToPriority(), DateSent = pop3Message.Date, Subject = pop3Message.Subject, IsBodyHtml = pop3Message.HtmlData != null, Body = pop3Message.HtmlData == null ? pop3Message.TextDataString : pop3Message.HtmlDataString, Attachments = pop3Message.GetAttachments() }; return message; } #region GetAttachments public static List GetAttachments(this Lesnikowski.Mail.SimpleMailMessage mail) { var attachments = new List(); var n = 0; for(var i = 0; i < mail.Attachments.Count; i++, n++) { attachments.Add(mail.Attachments[i].ToFileObject(n)); } if(mail.Visuals != null && mail.Visuals.Count > 0) { for(var i = 0; i < mail.Visuals.Count; i++, n++) { attachments.Add(mail.Visuals[i].ToFileObject(n)); } } return attachments; } #endregion #region ToFileObject public static FileObject ToFileObject(this Lesnikowski.Mail.MimeData mailAttachment, int n) { var buffer = mailAttachment.Data; var mimetype = mailAttachment.ContentType.TypeName + "/" + mailAttachment.ContentType.SubtypeName; var fileName = mailAttachment.ContentDisposition.FileName; string extension; if(String.IsNullOrEmpty(fileName)) { extension = mailAttachment.ContentType.SubtypeName; fileName = mailAttachment.ContentType.TypeName + n; } else { extension = fileName.Substring((fileName.LastIndexOf('.')), (fileName.Length - fileName.LastIndexOf('.'))); extension = extension.Remove(0, 1); fileName = fileName.Remove(fileName.LastIndexOf('.'), (fileName.Length - fileName.LastIndexOf('.'))); } return new FileObject { Content = buffer, Length = buffer.LongLength, MimeType = mimetype, Extension = extension, Name = fileName, FullName = fileName + "." + extension }; } #endregion #region ToPriority public static MailPriority ToPriority(this Lesnikowski.Mail.Importance importance) { switch(importance) { case Lesnikowski.Mail.Importance.High: return MailPriority.High; case Lesnikowski.Mail.Importance.Low: return MailPriority.Low; case Lesnikowski.Mail.Importance.Normal: return MailPriority.Normal; } return MailPriority.Normal; } #endregion #region ToStringSafe public static string ToStringSafe(this Lesnikowski.Mail.MailBox mailBox) { return mailBox != null ? mailBox.Address : null; } public static string ToStringSafe(this Lesnikowski.Mail.MailBoxList mailBoxList) { if(mailBoxList != null && mailBoxList.Count > 0) { var sb = new StringBuilder(); foreach(var mailBox in mailBoxList) { if(sb.Length > 0) { sb.Append(Pop3Message.AddressSeparator); } sb.Append(mailBox.ToStringSafe()); } return sb.ToString(); } return null; } #endregion } }