public Task BeginDownloadInboxAsync(CancellationToken token) { return(Task.Factory.StartNew(() => { using (var pop3 = new Pop3()) { ConnectAndLogin(pop3); var allUids = pop3.GetAll(); var chunkUids = Common.SplitList(allUids, 5); var builder = new MailBuilder(); foreach (var uids in chunkUids) { var items = new List <EmailItem>(); foreach (var uid in uids) { var bytes = pop3.GetHeadersByUID(uid); IMail email = builder.CreateFromEml(bytes); items.Add(ParseIMail(uid, email)); } ItemsDownloaded?.Invoke(this, items); } } }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default)); }
public async Task <List <Email> > GetPop3EmailHeaders(Model.UserInfo userInfo) { Pop3 pop3 = await ConnectPop3(userInfo); try { if (pop3 != null) { List <string> uids = pop3.GetAll(); MailBuilder builder = new MailBuilder(); foreach (string uid in uids) { var headers = pop3.GetHeadersByUID(uid); IMail mail = builder.CreateFromEml(headers); Email email = new Email { Uid = uid, Date = mail.Date ?? DateTime.MinValue, Subject = mail.Subject, From = string.Join(",", mail.From?.Select(s => s.Address)), }; emailHeaderList.Add(email); } } return(emailHeaderList); } finally { serverConnection.Add(pop3); } }
public async IAsyncEnumerable <Email> GetHeaders(UserInfo userInfo, [EnumeratorCancellation] CancellationToken token) { Pop3 pop3 = GetAvailableConnection(); try { if (_stackUids == null) { var uids = await Task.Run(() => pop3.GetAll()); _stackUids = new Stack <string>(uids); } if (_stackUids.Count > 0) { // If scroll is turned on, load only the qty requested by scroll int qtyLoad = !_settings.QtyEmailsByScroll.HasValue ? _stackUids.Count : _settings.QtyEmailsByScroll.Value; MailBuilder builder = new MailBuilder(); for (int i = 1; i <= qtyLoad; i++) { if (_stackUids.TryPop(out string uid)) { var headers = await Task.Run(() => pop3.GetHeadersByUID(uid)); IMail mail = builder.CreateFromEml(headers); Email email = new Email { Uid = uid, Date = mail.Date ?? DateTime.MinValue, Subject = mail.Subject, ListFrom = mail.From.Select(f => $"{f.Name} <{f.Address}>").ToList(), AttachmentsQuantity = mail.Attachments.Count }; yield return(email); } else { break; } } } } finally { _pop3Connections.Enqueue(pop3); } }
public Task <EmailEnvelope> GetHeaderByUID(string messageId) { var builder = new MailBuilder(); var headers = _pop3.GetHeadersByUID(messageId); var email = builder.CreateFromEml(headers); FreeClient(); return(Task.FromResult(new EmailEnvelope() { From = email.From.Select(x => x.Address).ToArray(), Subject = email.Subject, Date = email.Date })); }
private void GetHeader(string uid) { lock (EmailList) { var emailView = EmailList[uid.ToString()]; lock (pop) { MailBuilder builder = new MailBuilder(); var headers = pop.GetHeadersByUID(uid); IMail email = builder.CreateFromEml(headers); EmailViewManager.SetHeader(ref emailView, email.From.First().Name, email.Subject, email.Date?.ToString("MM/dd/yyyy hh:mm:ss")); ListChanged = true; } } }
void loadMail() { //lblLoadMail.Visible = true; //prgLoadMail.Visible = true; int numPacket = (pop3.GetAll()).Count; prgLoadMail.Maximum = numPacket; prgLoadMail.Value = 0; dtmail = new DataTable(); dtmail.Columns.Add("id", typeof(string)); dtmail.Columns.Add("người Gửi", typeof(string)); dtmail.Columns.Add("chủ đề", typeof(string)); dtmail.Columns.Add("thời gian", typeof(string)); //int i = 1; foreach (string uid in pop3.GetAll()) { imail = new MailBuilder().CreateFromEml(pop3.GetHeadersByUID(uid)); DataRow row = dtmail.NewRow(); row["id"] = uid; row["người Gửi"] = imail.From.ToString(); row["chủ đề"] = imail.Subject; row["thời gian"] = imail.Date.ToString(); if (prgLoadMail.Value >= prgLoadMail.Maximum) { prgLoadMail.Value = prgLoadMail.Minimum; } prgLoadMail.PerformStep(); this.Invoke((MethodInvoker) delegate() { lblLoadMail.Text = "Đã nhận " + prgLoadMail.Value + "/" + prgLoadMail.Maximum; }); //lblLoadMail.Text = "Đã nhận " + prgLoadMail.Value + "/" + prgLoadMail.Maximum; dtmail.Rows.Add(row); dtmail.AcceptChanges(); } dataGridView_Mail.DataSource = dtmail; }
/// <summary> /// Gets headers of the specified email message from server. Use <see cref="T:Limilabs.Mail.MailBuilder" /> class to create <see cref="T:Limilabs.Mail.IMail" /> object. /// </summary> /// <exception cref="T:Limilabs.Client.POP3.Pop3ResponseException">Throws <see cref="T:Limilabs.Client.POP3.Pop3ResponseException" /> on error response.</exception> /// <exception cref="T:System.Exception">Invalid uid provided.</exception> /// <param name="uid">Unique-id of the message to get.</param> /// <returns>Email message headers.</returns> public byte[] GetHeadersByUid(string uid) { return(_pop3.GetHeadersByUID(uid)); }
public int GetMails(short idBox, Boolean IsProtocolBox, string boxRecipient, Func <string, string, bool> headerExistHandler, string defaultSubject) { var builder = new MailBuilder(); var counter = 0; using (Pop3 mailClient = CreateMailClient()) { var currentStats = mailClient.GetAccountStat(); for (var messageCounter = (int)currentStats.MessageCount; messageCounter > 0 && counter < Pars.MaxMailsForSession; messageCounter--) { if (Pars.UserCanceled()) { return(counter); } var uid = mailClient.GetUID(messageCounter); LogAction("Get Headers Mail Uid:" + uid); byte[] headers = mailClient.GetHeadersByUID(uid); string headerHash = headers.ComputeSHA256Hash(); // Verifico se già presente, controlla header hash var email = builder.CreateFromEml(headers); LogAction(string.Format("Check Headers Mail Uid:{0} / Subject:{1} / Data:{2}", uid, email.Subject, email.Date)); if (headerExistHandler(headerHash, boxRecipient)) { continue; } //controlla se ho già scaricata nel drop folder if (MailInfo.CheckMailExist(Pars.DropFolder, headerHash)) { continue; } string outpathEml, outpathXml; Utils.GetDropFilenames(Pars.DropFolder, out outpathEml, out outpathXml); //mail info MailInfo mInfo = new MailInfo { Client = MailInfo.ClientType.Pop3, EmlFilename = outpathEml, IDPECMailBox = idBox, IsProtocolBox = IsProtocolBox, MailBoxRecipient = boxRecipient, MailUID = uid }; mInfo.Parse(email); mInfo.Body = "#"; mInfo.Size = GetMailSize(mailClient, messageCounter); mInfo.SaveAs(outpathXml); //download eml LogAction("Download Mail Uid:" + uid); byte[] eml = mailClient.GetMessageByUID(uid); File.WriteAllBytes(outpathEml, eml); //Aggiorna il Body //Pop3 deve forzatamente scaricare l'intero messaggio per ottenere il body della mail email = builder.CreateFromEml(eml); mInfo.HeaderHash = headerHash; mInfo.EmlHash = eml.ComputeSHA256Hash(); mInfo.Body = email.GetBodyAsHtml(); mInfo.UpdateStatus(MailInfo.ProcessStatus.Downloaded); mInfo.Save(); counter++; } return(counter); } }