[英]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.