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

How to get an ExchangeUser by email address with Outlook Object Library?

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

我有这个代码片段可以从当前用户那里获取经理,但是我想通过他们的电子邮件地址(而不是当前用户本身)获取 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()。准备好处理空值和异常。