「メールを受信したときに実行するプログラム」と「メールボックス内のフォルダ情報やinbox内にあるメールを表示する」サンプルコードです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook;
namespace OutlookExample
{
public class OutlookExample
{
public static void RunOutlookExample()
{
ApplicationClass appClass = new ApplicationClass();
appClass.NewMail += new ApplicationEvents_10_NewMailEventHandler(outLookApp_NewMailEx);
PrintInbox(appClass);
}
private static void outLookApp_NewMailEx(string EntryIDCollection)
{
// do something nice when mail is coming
}
public static void PrintInbox(ApplicationClass o)
{
// get items in my inbox (using MAPI)
NameSpace outlookNS = o.GetNamespace("MAPI");
MAPIFolder inboxFolder
= outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
foreach (MAPIFolder folder in outlookNS.Folders)
{
Console.WriteLine(folder.Name);
foreach (MAPIFolder subFolder in folder.Folders)
{
Console.WriteLine(" " + subFolder.Name);
}
}
// print out some basic info.
Console.WriteLine("You have {0} e-mails in your box", inboxFolder.Items.Count);
Console.WriteLine();
// save as txt (assume the mail format is html)
int i = 0;
foreach (object obj in inboxFolder.Items)
{
i++;
MailItem item = obj as MailItem;
if (item != null)
{
Console.WriteLine(" Received: {0}", item.ReceivedTime.ToString());
Console.WriteLine(" Sender: {0}", item.SenderName);
Console.WriteLine(" Subject: {0}", item.Subject);
item.SaveAs(@"c:\temp\message" + i + ".txt", OlSaveAsType.olHTML);
Console.WriteLine();
}
}
}
}
}
コメント