如何使用Outlook对象库通过电子邮件地址获取ExchangeUser?

问题描述

我正在做一个会议查找器,为此,我想阅读人们的日历。

我有这个代码片段来从当前用户那里获取经理,但是我想通过他们的电子邮件地址(而不是当前用户本身)来获取ExchangeUser。

Outlook.ExchangeUser manager = oApp.Session.CurrentUser.AddressEntry.GetExchangeUser().GetExchangeUserManager();

我该怎么做?

编辑: 这是对我有用的代码(尚未优化):

    private static outlook.application _oApp;

    static void Main(string[] args)
    {
        _oApp = GetApplicationObject();

        if (_oApp == null) {
            Console.WriteLine("Unable to connect to a running Outlook instance or create a new one");
            return;
        }

        Outlook.NameSpace oNamespace = _oApp.GetNamespace("MAPI");

        try
        {
            var user1Recipient = GetUserByEmailAddress("user1@example.tld");
            var user1 = user1Recipient.AddressEntry.GetExchangeUser();

            Console.WriteLine($"{user1.PrimarySmtpAddress}: {user1.Name}");

            try
            {
                Outlook.Folder folder = _oApp.Session.GetSharedDefaultFolder(user1Recipient,Outlook.OlDefaultFolders.olFolderCalendar) as Outlook.Folder;
                folder.display();
            }
            catch
            {
                Console.WriteLine($"Could not open {user1.Name}'s calendar!");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error {ex}");
        }
        finally
        {
            oNamespace.logoff();
        }

        Console.ReadLine();
    }

    private static Outlook.Recipient GetUserByEmailAddress(string email)
    {
        Outlook.Recipient recipient = null;

        try
        {
            Outlook._MailItem mailItem = _oApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook._MailItem;

            recipient = mailItem.Recipients.Add(email);

            recipient.Resolve();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex);
        }

        return recipient;
    }

    private static outlook.application GetApplicationObject()
    {
        outlook.application oApp = null;
        if (Process.GetProcessesByName("OUTLOOK").Any())
        {
            oApp = Marshal.GetActiveObject("outlook.application") as outlook.application;
        }
        else
        {
            oApp = new outlook.application();
            Outlook.NameSpace oNamespace = oApp.GetNamespace("MAPI");
            oNamespace.logon("","",Missing.Value,Missing.Value);
        }

        return oApp;
    }

解决方法

这对您有用吗?

在C#中:

public static Outlook.Recipient GetUserByMail(string mailAddress)
{
    Outlook.Recipient r = null;

    try
    {
        // TO DO: re-use existing application,if possible
        Outlook.Application OL = new Outlook.Application();

        Outlook._MailItem mail = 
           OL.CreateItem(Outlook.OlItemType.olMailItem) 
           as Outlook._MailItem;

        r = mail.Recipients.Add(mailAddress);

        r.Resolve();
    }
    catch (Exception)
    {
    }

    return r;
}

在VBA中:

Function GetUserByMail(mailAddress As String) As Recipient
    Dim myItem As mailItem
    Dim myRecipient As Recipient
    
    On Error GoTo ErrHandler
    
    Set myItem = Application.CreateItem(olMailItem)
 
    Set myRecipient = myItem.recipients.Add(mailAddress)
    
    myItem.recipients.ResolveAll
    
    Set GetUserByMail = myItem.recipients.item(1)
    On Error GoTo 0
    Exit Function
    
ErrHandler:
    Set GetUserByMail = Nothing
    Err.Clear
    On Error GoTo 0
End Function
,

将电子邮件地址传递到oApp.Session.CreateRecipient,先致电Recipient.Resolve,然后再致电Recipient.AddressEntry.GetExchangeUser()。准备处理null和异常。