C# - 从Outlook中的非默认帐户读取电子邮件 [英] C# -- Read Emails from non default accounts in Outlook

查看:703
本文介绍了C# - 从Outlook中的非默认帐户读取电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#(而不是我的默认收件箱)从Outlook中的特定文件夹和另一个收件箱中读取和打印电子邮件的内容。我发现在网络上很难找到这个例子,并且我自己失败了。我知道如何打印默认帐户的电子邮件,而不是额外的邮件。

I am trying to simply read and print the contents of emails from a specific folder and another inbox in Outlook using C# (not my default inbox). I'm finding it difficult to find examples of this on the web and have failed on my own. I know how to print the emails of the default account as well, just not additional ones.

我的代码只是遍历所有收件箱的列表,并打印出他们的名字。我想要阅读的是集合中的第一个元素。我感谢任何帮助这个问题。谢谢。

My code here simply iterates over a list of all the inboxes and prints their names out. The one I want to read is the first element in the collection. I appreciate any help with this issue. Thanks.

using System;
using System.Collections;
using Microsoft.Office.Interop.Outlook;

public class StorageReplies {

    public static void Main() {
        Application app = new Microsoft.Office.Interop.Outlook.Application(); 
        _NameSpace ns = app.GetNamespace("MAPI");

        Folders folders = ns.Folders;

        foreach(MAPIFolder f in folders) {
            Console.WriteLine(f.Name);
        }
    }
}


推荐答案

您可以获取每个文件夹的商店,然后调用 GetDefaultFolder 方法获取相应商店的收件箱文件夹,如下所示:

You could obtain the Store for each folder, and then call GetDefaultFolder method to obtain the inbox folder for the corresponding store like this:

foreach (MAPIFolder f in folders)
{
    MAPIFolder inbox_folder = f.Store.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

    foreach (MailItem item in inbox_folder.Items)
    {
        //Access item here
    }
}

但是,不用这样做,循环使用 商店 属性直接如下:

However, instead of doing that, it makes sense to loop through the Stores property directly like this:

Stores stores = ns.Stores;

foreach (Store store in stores)
{
    MAPIFolder inbox_folder = store.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

    foreach (MailItem item in inbox_folder.Items)
    {
        //Access item here
    }                
}

这篇关于C# - 从Outlook中的非默认帐户读取电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆