简体   繁体   English

使用C#阅读Outlook邮件

[英]Reading Outlook Mail with C#

I am using the following code as I attempt to connect to my Outlook mail. 当我尝试连接到Outlook邮件时,我正在使用以下代码。 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). 现在,我一定做错了,因为我尝试获取收件箱邮件,而我总是收到0封邮件(不是这种情况)。 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. 我的Outlook配置文件中有几个电子邮件帐户。 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). 帐户总数正确显示,我真正想要访问的帐户名称也是如此(索引1)。 Now, the problem is that I need to access a specific folder within that account. 现在,问题是我需要访问该帐户中的特定文件夹。 How do I do this? 我该怎么做呢?

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);
    }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM