读取本地outlook邮件内容

时间:2022-03-05 19:41:31
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication1
{
class Program
{ static void Main(string[] args)
{
var app = new Microsoft.Office.Interop.Outlook.Application();
var ns = app.GetNamespace("MAPI");
ns.Logon("Outlook", Type.Missing, false, false);
var inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
var myFolder = inbox.Folders["验证码"];
for(int i = ;i<= myFolder.Items.Count;i++)
{
var item = myFolder.Items[i]; //if(inbox.Items[i].Unread)
//{
writeLog(string.Format("unread={5},{0},{1},{2},{3},{4}"
,myFolder.Items[i].Subject.ToString()//标题
,myFolder.Items[i].Size
,myFolder.Items[i].SenderName
, myFolder.Items[i].ReceivedByName
,item.Body//邮件内容
,item.Unread
));
// }
}
Console.ReadLine();
} static public void writeLog(string strlog)
{
FileStream _fs;
StreamWriter _sw;
string sFilePath = "TestLog"; string sFilename = "info";
sFilename += DateTime.Now.ToString("yyyy-MM-dd-HH");
sFilename += ".log";
sFilename = sFilePath + "\\" + sFilename;
if (!Directory.Exists(sFilePath))
{
Directory.CreateDirectory(sFilePath);
} if (File.Exists(sFilename))
{
_fs = new FileStream(sFilename, FileMode.Append, FileAccess.Write); }
else
_fs = new FileStream(sFilename, FileMode.Create, FileAccess.Write);
_sw = new StreamWriter(_fs);
_sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " ---- " + strlog);
_sw.Flush();
_sw.Close();
_fs.Close(); }
}
}

需要增加引用

读取本地outlook邮件内容