获取 Outlook 2007 中当前用户的电子邮件地址
我有一个用 C# 编写的 Outlook 插件。
我想知道如何或是否可以获得当前用户的电子邮件地址?
谢谢
I have an Outlook add in written in C#.
I was wondering how or if I could get the email address of the current user?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 VS 2010、.NET 4.0、Outlook 2010 中测试:
Tested in VS 2010, .NET 4.0, Outlook 2010:
使用
Namespace.CurrentUser
: http://msdn.microsoft.com/en-us/library/bb220041(v=office.12).aspxUse
Namespace.CurrentUser
: http://msdn.microsoft.com/en-us/library/bb220041(v=office.12).aspx取决于“当前用户地址”的定义。
Appication.Session.CurrentUser
检索(返回Recipient
对象)。使用Recipient.Address
属性。但请注意,对于 Exchange 帐户 (Recipient.AddressEntry.Type == "EX"
),您将收到 EX 类型地址。要检索 SMTP 地址,请使用Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress
。准备好在出现错误时处理空值/异常。在扩展 MAPI 级别(C++ 或 Delphi)上,使用
IMAPISession::QueryIdentity
(您可以在 OutlookSpy - 我是其作者 - 单击 IMAPISession 按钮,然后单击 QueryIdentity)。然后,您可以读取PR_ADDRTYPE
属性(“EX”与“SMTP”)和PR_EMAIL_ADDRESS
(当PR_ADDRTYPE
= “SMTP”时)或(在如果是 Exchange)PR_SMTP_ADDRESS
(不保证存在)和PR_EMS_AB_PROXY_ADDRESSES
(具有 Exchange 地址的多值属性,包括所有代理(别名)地址、EX 和 SMTP)。如果配置文件中有多个帐户,则可以通过多个帐户发送或接收电子邮件。在这种情况下,请使用
MailItem.SendUsingAccount
(返回Account
对象,可以为 null - 在这种情况下使用Application.Session.CurrentUser
)。这对于接收、发送或撰写的电子邮件均有效(Application.ActiveInspector.CurrentItem
或Application.ActiveExplorer.ActiveInlineResponse
)。可以使用
Namespace.Accounts
集合(Application.Session.Accounts
)访问给定配置文件中的所有帐户。可以使用Account.SmtpAddress
属性访问帐户的地址。请注意,Outlook 对象模型仅公开邮件帐户。某些商店帐户(例如 PST)不在集合中,因为即使某些其他帐户(例如 POP3/SMTP)可以传送到该商店,它们也没有固有的用户身份。如果您想访问所有帐户,您可以使用 Redemption (我是其作者)及其 < a href="http://www.dimastr.com/redemption/rdosession.htm" rel="nofollow noreferrer">RDOSession.
Accounts
集合 (RDOAccounts 对象)。在扩展 MAPI 级别,帐户通过 IOlkAccountManager 界面。如果您单击 IOlkAccountManager 按钮,您可以在 OutlookSpy(我是其作者)中使用它。
PR_MAILBOX_OWNER_ENTRYID
属性仅由在线商店公开,在缓存商店中不可用)。您可以解析 Exchange 存储条目 ID 并从中提取 EX 类型地址。然后,您可以根据给定的 EX 地址构造 GAL 对象条目 id。您还可以使用兑换(我是其作者)及其RDOExchangeMailboxStore 对象及其Owner
属性。Depends on the definition of "the current user address".
Appication.Session.CurrentUser
(returnsRecipient
object). UseRecipient.Address
property. Note however that for an Exchange account (Recipient.AddressEntry.Type == "EX"
) you will receive an EX type address. To retrieve the SMTP address, useRecipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress
. Be prepared to handle nulls/exceptions in case of errors.On the Extended MAPI level (C++ or Delphi), use
IMAPISession::QueryIdentity
(you can test it in OutlookSpy - I am its author - click IMAPISession button, then QueryIdentity). You can then read thePR_ADDRTYPE
property ("EX" vs "SMTP") andPR_EMAIL_ADDRESS
(whenPR_ADDRTYPE
= "SMTP") or (in case of Exchange)PR_SMTP_ADDRESS
(not guaranteed to be present) andPR_EMS_AB_PROXY_ADDRESSES
(multivalued property with Exchange addresses, including all proxy (alias) addresses, EX and SMTP).In case of multiple accounts in the profile, an email can be sent or received through multiple accounts. In that case use
MailItem.SendUsingAccount
(returnsAccount
object, can be null - in that case useApplication.Session.CurentUser
). This is valid both for received, sent or emails being composed (Application.ActiveInspector.CurrentItem
orApplication.ActiveExplorer.ActiveInlineResponse
).All accounts in a given profile can be accessed using the
Namespace.Accounts
collection(Application.Session.Accounts
). Account's address can be accessed usingAccount.SmtpAddress
property.Note that the Outlook Object Model only exposes mail accounts. Some store accounts (such as PST) are not in the collection since they do not have an intrinsic user identity even if some other accounts (such as POP3/SMTP) can deliver to that store. If you want to access all accounts, you can use Redemption (I am its author) and its RDOSession.
Accounts
collection (RDOAccounts object).On the Extended MAPI level, the accounts are exposed through the IOlkAccountManager interface. You can play with it in OutlookSpy (I am its author) if you click the IOlkAccountManager button.
PR_MAILBOX_OWNER_ENTRYID
property is only exposed by the online store, it is not available in a cached store). You can parse the Exchange store entry id and extract the EX type address from it. You can then construct the GAL object entry id given the EX address. You can also access the store owner using Redemption (I am its author) and its RDOExchangeMailboxStore object and itsOwner
property.如果您使用普通的 POP/IMAP 服务器,您将获得以下电子邮件地址:
Application.Session.CurrentUser.AddressEntry.Address;
使用 Exchange Server,您可以通过以下方式获取电子邮件地址:
Application.Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
If you are using the normal POP/IMAP server, you get the e-mail address with:
Application.Session.CurrentUser.AddressEntry.Address;
With Exchange Server, you get the e-mail address with:
Application.Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
尝试
outlookObject.ActiveExplorer().Session.CurrentUser.Address
。为我工作过 VS10、Outlook 2007、.NET 4.0Try
outlookObject.ActiveExplorer().Session.CurrentUser.Address
. Worked for me on VS10, Outlook 2007, .NET 4.0任何路过的人,我强烈建议使用
Application.Session.CurrentUser.AddressEntry.Address
而不是使用任何东西
Application.ActiveExplorer().Session
,因为
ActiveExplorer()
可能会返回NullReferenceException
,例如通过 Outlook 打开电子邮件时。Anyone passing by, I'd strongly advice using
Application.Session.CurrentUser.AddressEntry.Address
instead of anything using
Application.ActiveExplorer().Session
,as
ActiveExplorer()
might return aNullReferenceException
, for instance when opening an email via Outlook.