我想創建一個服務,它將作爲服務器上的後臺服務運行。該服務一進入收件箱就會解析郵件。解析屬於其他帳戶的收件箱中的Outlook電子郵件
我們有一個電子郵件帳戶[email protected]。我們正在使用Outlook來檢查電子郵件。這是一項服務,所以Outlook不會一直在服務器上運行。我想爲此帳戶自動解析電子郵件。此帳戶不是我的電子郵件帳戶。我在C#程序中使用Microsoft.Office.Interop.outlook。
該程序正在運行,但它正在解析我的電子郵件收件箱中的電子郵件。我不知道如何指定特定的電子郵件來解析收件箱。需要知道新郵件到達時觸發的事件。我的程序從我的收件箱中解析了一半的電子郵件,但之後它拋出了對象空引用錯誤。
static void Main(string[] args)
{
Microsoft.Office.Interop.Outlook.Application myApp=new Microsoft.Office.Interop.Outlook.Application();
try
{
String Subject, Body, Createdate, Sender = null;
Microsoft.Office.Interop.Outlook.NameSpace mapinamespace = myApp.GetNamespace("MAPI");
mapinamespace.Logon(null, null,true,true);
Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapinamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
Microsoft.Office.Interop.Outlook.Accounts accounts = myApp.Session.Accounts;
try
{
foreach (Microsoft.Office.Interop.Outlook.Account account in accounts)
{
if (account.SmtpAddress == "[email protected]")
{
Console.Write(account);
}
else
{
Console.Write("Not found ---");
}
}
}
catch{
throw new System.Exception(string.Format("No account with amtp:{0} exists!"));}
foreach (object item in myInbox.Items)
{
try
{
var mail = item as MailItem;
if (mail != null)
{
//Creation date
Createdate = mail.CreationTime.ToString();
Console.WriteLine("CreationTime---" + Createdate);
//Grab the senders address
Sender = mail.SenderEmailAddress.ToString();
Console.WriteLine("Sender's E-mail---" + Sender);
//grab the Subject
Subject = mail.Subject.ToString();
Console.WriteLine("Subject--" + Subject);
//Grab the body
Body = mail.Body.ToString();
Console.WriteLine("Body---" + Body);
//Grab the Attachment
}
else
{
Console.Write("Error in mail---");
}
}
catch (System.Runtime.InteropServices.COMException e)
{
Console.Write(e);
}
}
}
catch (System.Runtime.InteropServices.COMException e)
{
Console.Write(e);
}
}
NewMailEx僅觸發配置文件中主郵箱中收到的郵件。它不會觸發委託郵箱中的郵件。 – 2015-01-20 19:57:53
謝謝。是的,我確實意識到我正在嘗試使用Exchange管理的API。 – user2897967 2015-01-23 16:44:25