C#实现QQ邮箱IMAP之邮件处理
2021/5/11 20:25:25
本文主要是介绍C#实现QQ邮箱IMAP之邮件处理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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(); } }
这篇关于C#实现QQ邮箱IMAP之邮件处理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具