/// <summary> /// 解析附件并且下载到本地目录 /// </summary> /// <param name="filePath">路径</param> /// <param name="messageFile">附件对象</param> /// <param name="UID"></param> /// <returns></returns> private static List <MailFile> GetFile(string filePath, MIME_Entity[] messageFile, string UID) { var resultList = new List <MailFile>(); foreach (MIME_Entity entity in messageFile) { var fileName = entity.ContentType.Param_Name; var fileByte = (MIME_b_SinglepartBase)entity.Body; var fileId = UID + "_" + fileName; DirFileHelper.CreateFile(filePath + fileId, fileByte.Data); var fileSize = DirFileHelper.GetFileSize(filePath + fileId); resultList.Add(new MailFile { FileId = fileId, FileName = fileName, FileSize = DirFileHelper.ToFileSize(fileSize) }); } return(resultList); }
/// <summary> /// 获取 /// </summary> /// <param name="account">配置</param> /// <param name="receiveCount">已收邮件数、注意:如果已收邮件数和邮件数量一致则不获取</param> /// <returns></returns> public static List <MailModel> Get(MailAccount account, int receiveCount) { try { var filePath = DirFileHelper.GetAbsolutePath("~/Resource/EmailFile/"); var resultList = new List <MailModel>(); using (POP3_Client pop3Client = new POP3_Client()) { pop3Client.Connect(account.POP3Host, account.POP3Port, account.Ssl); pop3Client.Login(account.Account, account.Password); var messages = pop3Client.Messages; if (receiveCount == messages.Count) { return(resultList); } for (int i = messages.Count - 1; receiveCount <= i; i--) { var messageItem = messages[i]; var messageHeader = Mail_Message.ParseFromByte(messageItem.MessageToByte()); resultList.Add(new MailModel() { UID = messageItem.UID, To = messageHeader.From == null ? "" : messageHeader.From[0].Address, ToName = messageHeader.From == null ? "" : messageHeader.From[0].DisplayName, Subject = messageHeader.Subject, BodyText = messageHeader.BodyHtmlText, Attachment = GetFile(filePath, messageHeader.GetAttachments(true, true), messageItem.UID), Date = messageHeader.Date, }); } } return(resultList); } catch (Exception ex) { throw ex; } }