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
Use Namespace.CurrentUser
: http://msdn.microsoft.com/en-us/library/bb220041(v=office.12).aspx
Tested in VS 2010, .NET 4.0, Outlook 2010:
var emailAddress = this.Application.ActiveExplorer().Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
Depends on the definition of "the current user address".
- The address of the primary account in Outlook can be retrieved from
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 - click IMAPISession button, then QueryIdentity). You can then read the PR_ADDRTYPE
property ("EX" vs "SMTP") and PR_EMAIL_ADDRESS
(when PR_ADDRTYPE
= "SMTP") or (in case of Exchange) PR_SMTP_ADDRESS
(not guaranteed to be present) and PR_EMS_AB_PROXY_ADDRESSES
(multivalued property will Exchange addresses, including all proxy (alias) addresses).
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 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 if you click the IOlkAccountManager button.
- In case of delegate Exchange stores, the store owner is not exposed through the Outlook Object Model. You can either use Extended MAPI (note that the 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 and its RDOExchangeMailboxStore object and its
Owner
property.
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;
Try outlookObject.ActiveExplorer().Session.CurrentUser.Address
. Worked for me on VS10, Outlook 2007, .NET 4.0
Anyone passing by, I'd strongly advice using Application.Session.CurrentUser.AddressEntry.Address
instead of anything using
Application.ActiveExplorer().Session
,
as ActiveExplorer()
might return a NullReferenceException
, for instance when opening an email via Outlook.
来源:https://stackoverflow.com/questions/4761521/get-the-email-address-of-the-current-user-in-outlook-2007