Get the email address of the current user in Outlook 2007
Asked Answered
U

6

19

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

Ultramontane answered 21/1, 2011 at 16:59 Comment(0)
S
7

Use Namespace.CurrentUser: http://msdn.microsoft.com/en-us/library/bb220041(v=office.12).aspx

Stefa answered 21/1, 2011 at 17:22 Comment(2)
Thats VBA though, not C#. I cant seem to find that in C#Ultramontane
you could try myEmailAddress = this.ActiveExplorer().Session.CurrentUser.EmailAddress; or try other solutions here: social.msdn.microsoft.com/forums/en-US/vsto/thread/…Stefa
K
14

Tested in VS 2010, .NET 4.0, Outlook 2010:

var emailAddress = this.Application.ActiveExplorer().Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
Killing answered 11/5, 2012 at 22:2 Comment(3)
Why not shorten it to var emailAddress = this.Application.Session.CurrentUser.AddressEntry.Address? You also need to check that AddressEntry.GetExchangeUser is valid - for a user with no Exchange in the profile, GetExchangeUser will return null.Papacy
@DmitryStreblechenko, I deliberately wrote it in long-form so that it would be obvious where it comes from. At the same time, I deliberately answered it with the minimum number of lines of code. I leave it to the OP to write defensive code in the context of his/her application.Killing
I think you are missing the point - ActiveExplorer() is not necessary and can return null. Get rid of it - use this.Application.Session.CurrentUser.AddressPapacy
S
7

Use Namespace.CurrentUser: http://msdn.microsoft.com/en-us/library/bb220041(v=office.12).aspx

Stefa answered 21/1, 2011 at 17:22 Comment(2)
Thats VBA though, not C#. I cant seem to find that in C#Ultramontane
you could try myEmailAddress = this.ActiveExplorer().Session.CurrentUser.EmailAddress; or try other solutions here: social.msdn.microsoft.com/forums/en-US/vsto/thread/…Stefa
P
6

Depends on the definition of "the current user address".

  1. The address of the primary account in Outlook can be retrieved from Appication.Session.CurrentUser (returns Recipient object). Use Recipient.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, use Recipient.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 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 with Exchange addresses, including all proxy (alias) addresses, EX and SMTP).

  1. In case of multiple accounts in the profile, an email can be sent or received through multiple accounts. In that case use MailItem.SendUsingAccount (returns Account object, can be null - in that case use Application.Session.CurentUser). This is valid both for received, sent or emails being composed (Application.ActiveInspector.CurrentItem or Application.ActiveExplorer.ActiveInlineResponse).

  2. All accounts in a given profile can be accessed using the Namespace.Accounts collection (Application.Session.Accounts). Account's address can be accessed using Account.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.

  1. 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 (I am its author) and its RDOExchangeMailboxStore object and its Owner property.
Papacy answered 5/9, 2015 at 18:58 Comment(3)
In case if an appointment is being created in the calendar, is there a way to use OOM to learn which email account will be associated with it? I.e. to map the calendar to the account when we have multiple accounts in the profile.Nonrestrictive
Not in Sure, read the appointment Parent property, cast it to the Folder object, read the Folder.Store property. Loop through the Namespace.Accounts collection and compare the value of the Account.DeliveryStore.EntryID property with the entry id of the appointment's parent store entry id.Papacy
Thanks! In the meantime, I found that AppointmentItem just created already contains one Recipient in its Recipients collection and this recipient has the email I need (so that the organizer is already initialized). Looks like an easier method (for this particular task). Or are there any drawbacks of this approach? It's provided that appointments in question are created by my own code so I don't have to deal with the case when the appointment has already existed and its recipients list could be non-default and cannot be relied on.Nonrestrictive
L
2

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;

Littoral answered 3/6, 2015 at 15:5 Comment(0)
T
0

Try outlookObject.ActiveExplorer().Session.CurrentUser.Address. Worked for me on VS10, Outlook 2007, .NET 4.0

Tati answered 4/9, 2014 at 11:6 Comment(0)
I
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.

Irrefutable answered 11/2, 2015 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.