简体   繁体   English

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

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

I'm currently doing a meeting finder and for that I want to read people's calendars.我目前正在做一个会议查找器,为此我想阅读人们的日历。

I have this snippet to get the manager from the current user, however I want to get the ExchangeUser by their email adress (not the current user itself).我有这个片段可以从当前用户那里获取经理,但是我想通过他们的电子邮件地址(而不是当前用户本身)获取 ExchangeUser。

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

How can I do that?我怎样才能做到这一点?

Edit: This is the code that worked for me (not optimized yet):编辑:这是对我有用的代码(尚未优化):

    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;
    }

Could this work for you?这对你有用吗?

In C#:在 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;
}

In VBA:在 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

Pass the email address to oApp.Session.CreateRecipient , call Recipient.Resolve , then call Recipient.AddressEntry.GetExchangeUser() .将电子邮件地址传递给oApp.Session.CreateRecipient ,调用Recipient.Resolve ,然后调用Recipient.AddressEntry.GetExchangeUser() Be prepared to handle nulls and exceptions.准备好处理空值和异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取Outlook.MeetingItem的发件人ExchangeUser和邮件地址 - How to get sender ExchangeUser and mailaddress for Outlook.MeetingItem Outlook帐户-如何获取Exchange电子邮件地址? - Outlook Accounts - How to get the Exchange Email address? 如何从 MS Outlook 2010 获取电子邮件地址? - How to get the email address from MS outlook 2010? 如何从Outlook获取发件人的电子邮件地址? - How to get the sender's email address from Outlook? 如何根据 Outlook 插件中的收件箱点击获取电子邮件地址? - How to get email address based on inbox click in outlook addin? Outlook Calendar Interop-如何获取发件人的电子邮件地址? - Outlook Calendar Interop - How to get Senders Email Address? 在 Outlook 2007 中获取当前用户的电子邮件地址 - Get the email address of the current user in Outlook 2007 通过c#获取Outlook电子邮件地址 - Get Outlook email address through c# C# VSTO Outlook 插件 - 如何使用传出 Z0C83ZEFA57C7831CEB7B24 获取发送方的 email 地址? - C# VSTO Outlook plugin - How can I get the email address of the sender of an outgoing email using Exchange? 如何使用 Google Oauth .NET 库获取用户的 email 地址 - How to get email address of a user using Google Oauth .NET library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM