public void DeleteDownloadMail(EmailDataInfo info) { var emlPath = this.GetMailDownloadPath(info.UIDL); var infoFilePath = emlPath + MailInfoFileExt; this.DeleteDownloadMail(emlPath, infoFilePath); }
public static EmailAttachmentInfo Create(Attachment attachment, EmailDataInfo dataInfo = null) { EmailAttachmentInfo info = new EmailAttachmentInfo(attachment); info.EmailDataInfo = dataInfo; info.From = dataInfo?.From; return(info); }
/// <summary> /// 获取邮件数据信息,并限定条件 /// </summary> /// <param name="startDate">晚于指定的接收日期的邮件</param> /// <param name="endDate">早于指定接收日期的邮件</param> /// <param name="titleKeywords">邮件标题中的关键词</param> public List <EmailDataInfo> GetMailDataInfos(DateTime?startDate = null, DateTime?endDate = null, String[] titleKeywords = null) { List <EmailDataInfo> emailDataInfos = new List <EmailDataInfo>(); List <Mail> mails = new List <Mail>(); List <MailInfo> mailInfos = new List <MailInfo>(); MailServer oServer = new MailServer(MailServerAddress, this.User, this.Password, true, ServerAuthType.AuthLogin, ServerProtocol.Imap4); oServer.Port = 993; MailClient oClient = new MailClient(LicenseCode); try { oClient.Connect(oServer); String condition = "ALL"; if (startDate.HasValue) { String startDateStr = this.GetDateString(startDate.Value); condition += " SINCE " + startDateStr; } if (endDate.HasValue) { String endDateStr = this.GetDateString(endDate.Value); condition += " SENTBEFORE " + endDateStr; } //官网上提供的一系列关键词的条件都无效,暂时使用本地筛选的方法。 https://www.emailarchitect.net/eagetmail/sdk/ //if (!String.IsNullOrWhiteSpace(titleKeyword)) //{ // condition += $" KEYWORD {titleKeyword}"; //} MailInfo[] result = oClient.SearchMail(condition); mailInfos.AddRange(result); foreach (var info in mailInfos) { //先获取邮件头部信息,此处邮件的信息并不完整,后续需要使用 GetMail 获取完整的邮件信息 var headerMailData = oClient.GetMailHeader(info); var headerMail = new Mail(LicenseCode); headerMail.Load(headerMailData); if (this.AccordTitleKeywords(headerMail.Subject, titleKeywords)) { EmailDataInfo dataInfo = new EmailDataInfo(); this.BuildEmailDataInfo(dataInfo, headerMail, info); emailDataInfos.Add(dataInfo); } } } finally { oClient.Quit(); oClient.Close(); } return(emailDataInfos); }
/// <summary> /// 保存邮件信息到本地 /// </summary> /// <param name="info"></param> private void SaveMailDataInfo(EmailDataInfo info) { String path = this.GetMailDownloadPath(info.MailInfo.UIDL); info.Mail.SaveAs(path, true); path += MailInfoFileExt; if (!File.Exists(path)) { File.Create(path).Dispose(); } DataSerializer.Serialize(info.MailInfo, path); }
/// <summary> /// 指定邮件接收时间是否在限定日期内,以及邮件主题是否含有指定关键词 /// </summary> /// <param name="dataInfo"></param> /// <param name="receiveDateStart"></param> /// <param name="receviceDataEnd"></param> /// <param name="titleKeywords"></param> /// <returns></returns> public Boolean AccordCondition(EmailDataInfo dataInfo, DateTime receiveDateStart, DateTime receviceDataEnd, String[] titleKeywords) { if (dataInfo.ReceviceTime > receiveDateStart && dataInfo.ReceviceTime < receviceDataEnd) { if (this.AccordTitleKeywords(dataInfo.Title, titleKeywords)) { return(true); } return(false); } else { return(false); } }
private void BuildEmailDataInfo(EmailDataInfo dataInfo, Mail mail, MailInfo info) { this.RemoveTrialInfo(mail); dataInfo.Mail = mail; dataInfo.Title = mail.Subject; dataInfo.MailInfo = info; dataInfo.Manager = this; dataInfo.AttachmentInfos.Clear(); foreach (var attachment in mail.Attachments) { var attachInfo = EmailAttachmentInfo.Create(attachment, dataInfo); String path = Path.Combine(this.AttachmentFloder, info.UIDL, attachInfo.Name); if (File.Exists(path)) { attachInfo.IsSaved = true; attachInfo.SavePath = path; } dataInfo.AttachmentInfos.Add(attachInfo); } }
/// <summary> /// 下载指定的邮件数据信息到本地 /// </summary> /// <param name="dataInfo"></param> public void DownloadEmailInfo(EmailDataInfo dataInfo) { MailServer oServer = new MailServer(MailServerAddress, this.User, this.Password, true, ServerAuthType.AuthLogin, ServerProtocol.Imap4); oServer.Port = 993; MailClient oClient = new MailClient(LicenseCode); try { oClient.Connect(oServer); var mail = oClient.GetMail(dataInfo.MailInfo); this.BuildEmailDataInfo(dataInfo, mail, dataInfo.MailInfo); this.SaveMailDataInfo(dataInfo); dataInfo.IsDownloadedMail = true; } finally { oClient.Quit(); oClient.Close(); } }
/// <summary> /// 绘制邮件信息项 /// </summary> private void CustomDrawItem(Object sender, DrawItemEventArgs e) { if (e.Index == -1 || this.DesignMode) { return; } var obj = this.Items[e.Index]; EmailDataInfo dataInfo = obj as EmailDataInfo; if (dataInfo == null) { return; } var g = e.Graphics; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; var forceColor = e.ForeColor; var backgroundColor = e.BackColor; var titleFont = e.Font; var formFont = e.Font; Int32 iconWidth = 30; var backgroudRect = new Rectangle(e.Bounds.X - 1, e.Bounds.Y - 1, e.Bounds.Width + 1, e.Bounds.Height + 2); Int32 textBeginX = e.Bounds.X + iconWidth; Int32 textBeginY = e.Bounds.Y + 3; var titleRect = new Rectangle(textBeginX, textBeginY, e.Bounds.Width - iconWidth, e.Bounds.Height); var fromRect = new Rectangle(textBeginX, textBeginY + 22, e.Bounds.Width - iconWidth, e.Bounds.Height); var timeRect = new Rectangle(e.Bounds.Width - 110, textBeginY + 24 + 20, e.Bounds.Width - iconWidth, e.Bounds.Height); var readFlagRect = new Rectangle(5, 7, 20, 20); var attachmentFlagRect = new Rectangle(5, 30, 20, 20); if (this.MouseHoverItemIndex == e.Index) { backgroundColor = Color.FromArgb(224, 224, 224); } if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { backgroundColor = Color.FromArgb(200, 200, 200); } Brush backBrush = new SolidBrush(backgroundColor); g.FillRectangle(backBrush, backgroudRect); //左上边绘制邮件阅读标识 var readFlagImage = dataInfo.IsRead ? this.ItemReadFlagImage : this.ItemNotReadFlagImage; if (readFlagImage != null) { g.DrawImage(readFlagImage, readFlagRect); } //左下绘制附件标识 if (dataInfo.HadAttachment) { var attachmentFlagImage = this.ItemAttachmentFlagImage; if (dataInfo.IsDownloadAttachment) { attachmentFlagImage = this.ItemAttachmentDownloadFlagImage; } if (attachmentFlagImage != null) { g.DrawImage(attachmentFlagImage, attachmentFlagRect); } } //绘制邮件标题 Title 属性 TextRenderer.DrawText(g, dataInfo.Title, this.ItemTitleFont, titleRect, forceColor, TextFormatFlags.EndEllipsis); //绘制邮件来源 From 属性 TextRenderer.DrawText(g, "By:" + dataInfo.From, this.ItemSenderFont, fromRect, forceColor, TextFormatFlags.EndEllipsis); //绘制接收时间 String receiveTime = dataInfo.ReceviceTime.ToString("yyyy-MM-dd HH:mm"); TextRenderer.DrawText(g, receiveTime, this.ItemReceiveTimeFont, timeRect, forceColor, TextFormatFlags.EndEllipsis); backBrush.Dispose(); }
/// <summary> /// 获取本地已下载的邮件信息 /// </summary> /// <param name="startDate">晚于指定的接收日期的邮件</param> /// <param name="endDate">早于指定接收日期的邮件</param> /// <param name="titleKeywords">邮件标题中的关键词</param> /// <param name="isDelete">是否删除不满足条件的本地邮件</param> /// <returns></returns> public List <EmailDataInfo> GetDownloadMailDataInfos(DateTime?startDate = null, DateTime?endDate = null, String[] titleKeywords = null, Boolean isDelete = false) { List <EmailDataInfo> dataInfos = new List <EmailDataInfo>(); if (!Directory.Exists(this.DownloadFloder)) { return(dataInfos); } var fileNames = Directory.GetFiles(this.DownloadFloder, "*" + MailFileExt); if (fileNames.Length <= 0) { return(dataInfos); } foreach (var filename in fileNames) { Mail mail = new Mail(LicenseCode); try { mail.Load(filename, true); var infoFilePath = filename + MailInfoFileExt; if (startDate.HasValue) { if (mail.ReceivedDate < startDate) { if (isDelete) { goto DELTE; } else { continue; } } } if (endDate.HasValue) { if (mail.ReceivedDate > endDate && isDelete) { if (isDelete) { goto DELTE; } else { continue; } } } if (titleKeywords != null && titleKeywords.Length > 0) { if (!this.AccordTitleKeywords(mail.Subject, titleKeywords)) { if (isDelete) { goto DELTE; } else { continue; } } } MailInfo info = null; if (File.Exists(infoFilePath)) { info = DataSerializer.Deserialize <MailInfo>(infoFilePath); } else { info = new MailInfo(); var uldl = Path.GetFileNameWithoutExtension(filename); info.UIDL = uldl; } EmailDataInfo dataInfo = new EmailDataInfo(); this.BuildEmailDataInfo(dataInfo, mail, info); dataInfo.IsDownloadedMail = true; dataInfos.Add(dataInfo); continue; DELTE: this.DeleteDownloadMail(filename, infoFilePath); } catch { } } return(dataInfos); }