public void DealEmail(string fjType = ".db") { IMAP_Client IMAPServer = new IMAP_Client(); try { //连接qq邮箱 IMAPServer.Connect(popServer, popPort); IMAPServer.Login(accout, pass); //获取邮件 IMAPServer.SelectFolder("INBOX"); //获取未读邮件id int[] unseen_ids = IMAPServer.Search(false, Encoding.UTF8, new IMAP_Search_Key_Unseen()); //没有未读邮件则返回 if (unseen_ids.Length == 0) continue; //未读邮件集合 IMAP_t_SeqSet sequence = IMAP_t_SeqSet.Parse(string.Join(",", unseen_ids)); //所需未读邮件集合参数 var items = new IMAP_t_Fetch_i[] { new IMAP_t_Fetch_i_Envelope(), new IMAP_t_Fetch_i_Uid(), new IMAP_t_Fetch_i_Flags(), new IMAP_t_Fetch_i_Flags(), new IMAP_t_Fetch_i_InternalDate(), new IMAP_t_Fetch_i_Rfc822() }; //遍历邮件 IMAPServer.Fetch(false, sequence, items, (s, e) => { try { var email = e.Value as IMAP_r_u_Fetch; if (email.Rfc822 != null) { //处理邮件附件 email.Rfc822.Stream.Position = 0; var mine = Mail_Message.ParseFromStream(email.Rfc822.Stream); email.Rfc822.Stream.Close(); if (mine.Attachments.Length > 0) { List<MIME_Entity> list = new List<MIME_Entity>(mine.Attachments); foreach (var att in list) { if (att.ContentType.Param_Name.Contains(fjType)) { var part = att.Body as MIME_b_SinglepartBase; //路径被URL编码了,格式为:UTF-8‘‘附件名 string filename = att.ContentType.Param_Name; if(filename.Contains("UTF-8")) filename= HttpUtility.UrlDecode(filename.Substring(7)); //下载到指定地址 string filePath = Path.Combine(rootPath, Path.GetFileNameWithoutExtension(filename)); File.WriteAllBytes(filePath, part.Data); } } } //邮件信息 //var Title = email.Envelope.Subject; //var CreateTime = email.InternalDate.Date; //var BodyText = mine.BodyText; //var BodyHtmlText = mine.BodyHtmlText; //处理完的邮件标记为已读 IMAP_t_SeqSet sequenceSet = IMAP_t_SeqSet.Parse(string.Join(",", email.SeqNo)); IMAPServer.StoreMessageFlags(false, sequenceSet, IMAP_Flags_SetType.Add, new IMAP_t_MsgFlags(new string[] { IMAP_t_MsgFlags.Seen })); } } catch (Exception ex) { throw ex; } }); } catch (Exception ex) { throw ex; } finally { IMAPServer.Disconnect(); } }