C# 如何使用Outlook对象库通过电子邮件获取ExchangeUser地址?
我目前正在做一个会议查找程序,为此我想阅读人们的日历 我有这个片段可以从当前用户那里获取经理,但是我想通过他们的电子邮件地址(而不是当前用户本身)获取ExchangeUser 我该怎么做 编辑: 这是适用于我的代码(尚未优化):C# 如何使用Outlook对象库通过电子邮件获取ExchangeUser地址?,c#,.net,.net-core,outlook,exchange-server,C#,.net,.net Core,Outlook,Exchange Server,我目前正在做一个会议查找程序,为此我想阅读人们的日历 我有这个片段可以从当前用户那里获取经理,但是我想通过他们的电子邮件地址(而不是当前用户本身)获取ExchangeUser 我该怎么做 编辑: 这是适用于我的代码(尚未优化): 这对你有用吗 在C#中: 在VBA中: Function GetUserByMail(mailAddress As String) As Recipient Dim myItem As mailItem Dim myRecipient As Recipi
这对你有用吗 在C#中: 在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
这对你有用吗 在C#中: 在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()
。准备好处理空值和异常。将电子邮件地址传递给oApp.Session.CreateRecipient
,调用Recipient.Resolve
,然后调用Recipient.AddressEntry.GetExchangeUser()
。准备好处理空值和异常。@MaciejLos我无法将其转换为C#您可以使用;)顺便说一句:VBA/VB.net很容易转换成C。如果有此帮助,请告诉我。@MaciejLos我基本上已经尝试了所有这些方法,但每种方法都显示错误,并且不进行转换correctly@MaciejLos我不能把它翻译成C(你可以用;)顺便说一句:VBA/VB.net很容易转换成C。如果有帮助,请告诉我。@MaciejLos我基本上已经试过了,但每一个都显示错误,无法正确转换。不幸的是,没有,我试图将其重建为C#,因为我几乎不懂VB。然而,像olMailItem
这样的东西在C#中并不存在。@Steven2105,olMailItem
是一个枚举。请参阅:没有理由使用CreateItem
创建临时项-您可以使用名称空间。CreateRecipient
/收件人。解析
。谢谢:)您的C为我工作,使用@DmitryStreblechenko的答案,我也能够获得ExchangeUser。不幸的是,没有,我试图将它重建为C#,因为我几乎不懂VB。然而,像olMailItem
这样的东西在C#中并不存在。@Steven2105,olMailItem
是一个枚举。请参阅:没有理由使用CreateItem
创建临时项目-您可以使用名称空间。CreateRecipient
/收件人。解析
。谢谢:)您的C为我工作并使用@DmitryStreblechenko的答案,我也能得到交换者谢谢:)通过@AxelKemper的回答,我能得到接收者,通过你的最后一个片段,我也能得到交换者谢谢:)通过@AxelKemper的回答,我能得到接收者,通过你的最后一个片段,我也能得到交换者
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;
}
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