Reading Outlook Mail with C#

我是研究僧i 提交于 2019-12-29 04:42:08

问题


I am using the following code as I attempt to connect to my Outlook mail. Now, I must be doing something wrong because I try to get the inbox mails and I always get 0 mails (when this is not the case). This is my code

 Microsoft.Office.Interop.Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
 nameSpace.Logon("", "", Missing.Value, Missing.Value);

 inboxFolder = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
 Console.WriteLine("Folders: {0}", inboxFolder.Folders.Count);

I have several email accounts in my Outlook profile. When I write the following

Console.WriteLine("Accounts: {0}",nameSpace.Accounts.Count);
Console.WriteLine("Name: {0}", nameSpace.Accounts[1].DisplayName);

The total number of accounts is displayed correctly, and so is the name of the account i really want to access (index 1). Now, the problem is that I need to access a specific folder within that account. How do I do this?


回答1:


I could solve this! It was quite easy actually. Here is how I could access the desired folder:

// my-account@myserver.com is the name of my account
// Unsent mails is the name of the folder I wanted to access
inboxFolder = nameSpace.Folders["my-account@myserver.com"].Folders["Unsent mails"];

foreach (Microsoft.Office.Interop.Outlook.MailItem mailItem in inboxFolder.Items)
{
    if (mailItem.UnRead) // I only process the mail if unread
    {
        Console.WriteLine("Accounts: {0}", mailItem.Body);
    }    
}


来源:https://stackoverflow.com/questions/8368380/reading-outlook-mail-with-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!