c#收取获取outlook中的邮件内容

界面如下:

 

 

 

直接上代码如下:

Microsoft.Office.Interop.Outlook.Application myOutlookApp = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.NameSpace myNameSpace = myOutlookApp.GetNamespace("MAPI");
            //本地邮箱
            Microsoft.Office.Interop.Outlook.MAPIFolder myFolderInbox = myNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);//获取收件箱对象,如获取其他箱可在参数中控制
            //Microsoft.Office.Interop.Outlook.MAPIFolder myFolder = myFolderInbox.Folders["xx"];//“xx”为收件箱下的一个文件夹
            //Microsoft.Office.Interop.Outlook.MAPIFolder MyParentFolder = myFolderInbox.Parent as Microsoft.Office.Interop.Outlook.MAPIFolder;//获取收件箱上一级的文件夹对象,以次来获取与收件箱同级的文件夹
            //Microsoft.Office.Interop.Outlook.MAPIFolder MyNewFolder = MyParentFolder.Folders["yy"];//“yy”为与收件箱同级的文件夹

            Microsoft.Office.Interop.Outlook.Items myMails = myFolderInbox.Items as Microsoft.Office.Interop.Outlook.Items;
            Console.WriteLine("mail Count:" + myMails.Count.ToString());
            int showCount = 0;
            for (int index = myMails.Count; index >0; index--) {
                //倒序才是从最近的收到的邮件显示
                Microsoft.Office.Interop.Outlook.MailItem myMail = myMails[index] as Microsoft.Office.Interop.Outlook.MailItem;
                Console.WriteLine("======================================================");
                Console.WriteLine("Subject:" + myMail.Subject.ToString());
                Console.WriteLine("ReceivedTime:" + myMail.ReceivedTime.ToString());
                Console.WriteLine("Body:" + myMail.Body.ToString().Substring(0,10));
                showCount++;
                if (showCount > 10) break;//只显示10封
            }

记得添加引 using Microsoft.Office.Interop.Outlook;

说明,

如果是 office2010用14.0.0.0  

如果是  office2013/2016用15.0.0.0 

 

代码如下:https://files.cnblogs.com/files/q149072205/ReadEmailDemo.rar

posted @ 2021-01-13 17:09  为乐而来  阅读(2938)  评论(0编辑  收藏  举报