/// <summary> /// Get text body of mail from cache or server and update cache if available /// </summary> /// <param name="summary">Mail summary</param> /// <param name="inbox">Mailbox that contains mail</param> /// <returns>Body text</returns> private string GetBodyText(IMessageSummary summary, IMailFolder inbox) { //Get from cache var cachedText = CacheService?.GetCachedMail(summary.UniqueId.Id); if (cachedText != null) { return(cachedText.BodyText); } //Download from server var bodyText = ""; if (summary.TextBody != null) { var text = (TextPart)inbox.GetBodyPart(summary.UniqueId, summary.TextBody); bodyText = text.Text.Trim(); } else if (summary.HtmlBody != null) { //TODO: Read html body bodyText = ""; } //Update cache CacheService?.UpdateCachedMailPart(summary.UniqueId.Id, "BodyText", bodyText); return(bodyText); }
public SaveResult saveAttachment(IMailFolder folder, IMessageSummary summary, BodyPartBasic attachment) { var result = SaveResult.Error; var entity = folder.GetBodyPart(summary.UniqueId, attachment); if (entity is MimePart) { var part = (MimePart)entity; var log = logAttachment(summary.UniqueId, part.FileName, part.ContentType); if (isIgnoreableAttachment(part.ContentType, part.FileName)) { log("Ignoring attachment"); result = SaveResult.Ignore; } else if (IsMimeTypeAcceptable(part.ContentType)) { log("MIME type ok"); result = downloader.downloadAttachment( part.Content, this.fileNamer(folder)(part)(summary) ); } else { log("MIME type bad"); } } return(result); }
void RenderMultipartRelated(IMailFolder folder, UniqueId uid, BodyPartMultipart bodyPart) { // download the entire multipart/related for simplicity since we'll probably end up needing all of the image attachments anyway... var related = folder.GetBodyPart(uid, bodyPart) as MultipartRelated; RenderMultipartRelated(related); }
public async Task Attachment(MailWindow inboxPage, User user, ConfigModel conf, string Atch, string destination) { using (ImapClient client = new ImapClient()) { await client.ConnectAsync(conf.ImapServer, conf.ImapPort); client.Authenticate(user.Mail, user.Password); IMailFolder Folder = client.GetFolder(ListMessages.fold); await Folder.OpenAsync(FolderAccess.ReadWrite); IList <IMessageSummary> atc = Folder.Fetch(new[] { LastOpenId }, MessageSummaryItems.Body); var multipart = (BodyPartMultipart)atc.First().Body; var attachment = multipart.BodyParts.OfType <BodyPartBasic>().FirstOrDefault(x => x.FileName == Atch); TransferProgress progress = new TransferProgress(); Downloads FileDownload = new Downloads(); FileDownload.Show(); var file = Folder.GetBodyPart(LastOpenId, attachment, default, progress);
public async Task OpenText(Message m, OpenMessage Page, User user, ConfigModel conf) { using (ImapClient client = new ImapClient()) { List <string> atc = new List <string>(); await client.ConnectAsync(conf.ImapServer, conf.ImapPort); client.Authenticate(user.Mail, user.Password); IMailFolder Folder = client.GetFolder(ListMessages.fold); await Folder.OpenAsync(FolderAccess.ReadWrite); IList <IMessageSummary> msg = Folder.Fetch(new[] { m.ID }, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.Envelope); LastOpenId = msg.First().UniqueId; var bodyHTML = (TextPart)Folder.GetBodyPart(msg.First().UniqueId, msg.First().HtmlBody); Page.Dispatcher.Invoke(() => { Page.Attachments.Children.Clear(); }); Folder.SetFlags(m.ID, MessageFlags.Seen, true); m.MessageColor = new SolidColorBrush(Colors.White); foreach (BodyPartBasic attachment in msg.First().Attachments) { Button bt = new Button { Content = attachment.FileName, }; Page.Dispatcher.Invoke(() => { bt.Click += new RoutedEventHandler(new MailWindow(user, conf).OpenAttachment); Page.Attachments.Children.Add(bt); }); } if (m != null) { Page.Dispatcher.Invoke(() => { Page.Info.Text = ($"Subject: {msg.First().Envelope.Subject} \n\rFrom {msg.First().Envelope.From} at {msg.First().Envelope.Date} to {msg.First().Envelope.To}"); Page.Body.NavigateToString("<html><head><meta charset='UTF-8'></head>" + bodyHTML.Text + "</html>"); Page.MailBody.Visibility = 0; }); } client.Disconnect(true); } }
protected async Task GetEmailAndSaveDataAsync(IEnumerable <BodyPartBasic> attachments, IMailFolder folder, UniqueId uid) { foreach (var attachment in attachments) { MimeEntity part = folder.GetBodyPart(uid, attachment); var fileName = part.ContentDisposition?.FileName ?? attachment.ContentType.Name; if (string.IsNullOrEmpty(fileName) || !fileName.EndsWith(FileExtensions.CSV, StringComparison.InvariantCultureIgnoreCase)) { continue; } var bytes = await GetBytesArrayToRead((MimePart)part); await ReadStreamAsync(bytes); } }
/// <summary> /// Fetch unprocessed transaction ids from email messages /// </summary> public IList <MessageInfo> ParseTransaction() { var headers = new HashSet <string>(new[] { ppEmailTypeHeader }); var ids = new List <MessageInfo>(); var q = SearchQuery.FromContains("paypal.com") .And(SearchQuery.Not(SearchQuery.HasGMailLabel(successLabel))) .And(SearchQuery.Not(SearchQuery.HasGMailLabel(failureLabel))); // Fetch messages metadata and filter out // messages without the required email type codes var msgs = _inbox.Fetch(_inbox.Search(q), MessageSummaryItems.Full, headers); var filtered = msgs.Where(m => m.Headers.Contains(ppEmailTypeHeader) && IsCorrectCode(m.Headers[ppEmailTypeHeader])).ToArray(); log.Debug("Found {0} unprocessed messages from paypal.com w/ filter codes: [ {1} ]", filtered.Length, string.Join(", ", _filterCodes)); // Download text body and parse transaction ids foreach (var msg in filtered) { TextPart textBody = _inbox.GetBodyPart(msg.UniqueId, msg.TextBody) as TextPart; var id = ParseTransactionId(textBody.Text); if (id == null) { log.Warn("Failed to find any transaction id in message <{0}> with subject <{1}>", msg.Envelope.MessageId, msg.Envelope.Subject); continue; } log.Info("Found transaction <{0}> in message <{1}>", id, msg.Envelope.MessageId); ids.Add(new MessageInfo { TransactionId = id, MessageId = msg.UniqueId.Id }); } return(ids); }
void RenderText(IMailFolder folder, UniqueId uid, BodyPartText bodyPart) { var entity = folder.GetBodyPart(uid, bodyPart); RenderText((TextPart)entity); }