1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.IO;
 7 namespace ConsoleApplication1
 8 {
 9     class Program
10     {
11 
12       
13         static void Main(string[] args)
14         {
15             var app = new Microsoft.Office.Interop.Outlook.Application();
16             var ns = app.GetNamespace("MAPI");
17             ns.Logon("Outlook", Type.Missing, false, false);
18             var inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
19             var myFolder = inbox.Folders["验证码"];
20             for(int i = 1;i<= myFolder.Items.Count;i++)
21             {
22                 var item = myFolder.Items[i];
23                 
24                //if(inbox.Items[i].Unread)
25                //{
26                 writeLog(string.Format("unread={5},{0},{1},{2},{3},{4}"
27                     ,myFolder.Items[i].Subject.ToString()//标题
28                     ,myFolder.Items[i].Size
29                      ,myFolder.Items[i].SenderName
30                       , myFolder.Items[i].ReceivedByName
31                       ,item.Body//邮件内容
32                       ,item.Unread
33                      ));
34                // }
35             }
36             Console.ReadLine();
37         }
38 
39   
40         static public void writeLog(string strlog)
41         {
42             FileStream _fs;
43             StreamWriter _sw;
44             string sFilePath = "TestLog";
45 
46             string sFilename = "info";
47             sFilename += DateTime.Now.ToString("yyyy-MM-dd-HH");
48             sFilename += ".log";
49             sFilename = sFilePath + "\\" + sFilename;
50             if (!Directory.Exists(sFilePath))
51             {
52                 Directory.CreateDirectory(sFilePath);
53             }
54 
55             if (File.Exists(sFilename))
56             {
57                 _fs = new FileStream(sFilename, FileMode.Append, FileAccess.Write);
58 
59             }
60             else
61                 _fs = new FileStream(sFilename, FileMode.Create, FileAccess.Write);
62             _sw = new StreamWriter(_fs);
63             _sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " ---- " + strlog);
64             _sw.Flush();
65             _sw.Close();
66             _fs.Close();
67 
68 
69         }
70     }
71 }

View Code

需要增加引用

 

版权声明:本文为zhmf-4459原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zhmf-4459/p/9036906.html