代码之家  ›  专栏  ›  技术社区  ›  Steven2105

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

  •  0
  • Steven2105  · 技术社区  · 5 年前

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

    我有这个片段可以从当前用户那里获取经理,但我想通过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;
        }
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   Axel Kemper    5 年前

    这对你有用吗?

    在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
    
        2
  •  1
  •   Dmitry Streblechenko    5 年前

    将电子邮件地址传递给 oApp.Session.CreateRecipient ,呼叫 Recipient.Resolve ,然后致电 Recipient.AddressEntry.GetExchangeUser() .准备好处理空值和异常。