引用Microsoft.Office.Interop.Outlook
先找到指定的信箱和信箱目錄
using Outlook = Microsoft.Office.Interop.Outlook;
private static Outlook.Folder GetMailFolder(Outlook.Application app, string BoxName, string FolderName)
{
Outlook.Store st = null;
foreach (Outlook.Store s in app.Session.Stores)
if (s.DisplayName == BoxName)
st = s;
foreach (Outlook.Folder f in st.GetRootFolder().Folders)
if (f.Name == FolderName)
return f;
return null;
}
上列的Method找到的Folder內讀取信件處理
int count = From.Items.Count;
for (int i = count; i > 0; i--)
{
Outlook.MailItem mail = From.Items[i] as Outlook.MailItem;
//todo
}
寫成function:
public IEnumerable<MailInfo> GetMails(SettingID BoxName, SettingID FolderName)
{
var From = GetFolder(BoxName, FolderName);
int count = From.Items.Count;
for (int i = count; i > 0; i--)
{
if (!(From.Items[i] is Outlook.MailItem mail))
From.Items[i].Delete();
else
yield return new MailInfo(mail);
}
}
ps. 若想關閉outlook, 宣告的Outlook.Application
使用完執行.Quit()
, 若不想關就不用執行
Taiwan is a country. 臺灣是我的國家