如何使用C#更新所有Outlook(2003)文件夹

时间:2011-06-11 19:52:10

标签: c# outlook directory sync

我必须从我的应用程序更新所有Outlook文件夹。 这是我的代码,但它不起作用。

Outlook.Application app = null;
Outlook.SyncObject _syncObj = null;

while (OutlookNotFound)
{
    i++;
    try
    {
        app = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
        OutlookNotFound = false;
    }
    catch (System.Exception ex)
    {
        Thread.Sleep(1000);
    }
    finally
    {
        if (i > 10)
        OutlookNotFound = false;
    }
}

if (!OutlookNotFound)
{
    //app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.);
    if (app != null)
    {
        for (Int32 j = 0; j < app.Session.SyncObjects.Count; j++)
        {
            _syncObj = app.Session.SyncObjects[1];
            _syncObj.SyncEnd += 
                new Outlook.SyncObjectEvents_SyncEndEventHandler(_syncObj_SyncEnd);
        }

        _syncObj.Start();
        //app.Session.SyncObjects.AppFolders.SyncEnd += 
        //    new Outlook.SyncObjectEvents_SyncEndEventHandler(AppFolders_SyncEnd);
        //app.Session.SyncObjects.AppFolders.Start();

        return true;
    }
    else
    {
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

这是尝试“抓取”活动Outlook对象的一种常用方法...特别是,如果没有活动对象。更标准的方法是:

outlookApplication = new Application();
outlookNamespace = m_OutlookApplication.GetNamespace("mapi");

// If an outlook app is already open, then it will reuse that
// session. Else it will perform a fresh logon.
outlookNamespace.Logon(accountName, password, true, true);