6

I need to be able to get the currently logged in user's email address with C# code.

I need the full address and not just the assumed email account (eg user@localdomain.com.au), although this will work for most clients.

Any help will be greatly appreciated.

jdphenix
  • 15,022
  • 3
  • 41
  • 74
Matthew kingston
  • 185
  • 1
  • 3
  • 12
  • I'm afraid i don't have an attempt at the moment the only reference i could find was vb and need a point in the right direction – Matthew kingston Sep 04 '13 at 05:42
  • Almost all VB constructs can be converted to C# pretty readily - also take a look around SO, questions like http://stackoverflow.com/questions/4761521/get-the-email-address-of-the-current-user-in-outlook-2007 might apply for you – jdphenix Sep 04 '13 at 05:46

3 Answers3

7

Try this, from http://msdn.microsoft.com/en-us/library/ff462091.aspx:

using System;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace OutlookAddIn1
{
    class Sample
    {
        public static void DisplayAccountInformation(Outlook.Application application)
        {

            // The Namespace Object (Session) has a collection of accounts.
            Outlook.Accounts accounts = application.Session.Accounts;

            // Concatenate a message with information about all accounts.
            StringBuilder builder = new StringBuilder();

            // Loop over all accounts and print detail account information.
            // All properties of the Account object are read-only.
            foreach (Outlook.Account account in accounts)
            {

                // The DisplayName property represents the friendly name of the account.
                builder.AppendFormat("DisplayName: {0}\n", account.DisplayName);

                // The UserName property provides an account-based context to determine identity.
                builder.AppendFormat("UserName: {0}\n", account.UserName);

                // The SmtpAddress property provides the SMTP address for the account.
                builder.AppendFormat("SmtpAddress: {0}\n", account.SmtpAddress);

                // The AccountType property indicates the type of the account.
                builder.Append("AccountType: ");
                switch (account.AccountType)
                {

                    case Outlook.OlAccountType.olExchange:
                        builder.AppendLine("Exchange");
                        break;

                    case Outlook.OlAccountType.olHttp:
                        builder.AppendLine("Http");
                        break;

                    case Outlook.OlAccountType.olImap:
                        builder.AppendLine("Imap");
                        break;

                    case Outlook.OlAccountType.olOtherAccount:
                        builder.AppendLine("Other");
                        break;

                    case Outlook.OlAccountType.olPop3:
                        builder.AppendLine("Pop3");
                        break;
                }

                builder.AppendLine();
            }

            // Display the account information.
            System.Windows.Forms.MessageBox.Show(builder.ToString());
        }
    }
}
Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
3

Get email id from outlook

createdby = mailItem.UserProperties.Session.CurrentUser.Address;

or, we can use this

if (app.Session.CurrentUser.AddressEntry.Type == "EX") {
    createdby = app.Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
} else {
    createdby = app.Session.CurrentUser.AddressEntry.Address;
}
Edwin
  • 1,135
  • 2
  • 16
  • 24
1

Using C# code, Get the From Email Id from outlook 2013, 2016.

using System;
using System.Net;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.IO;
using System.Net.Mail;

public string CreateByEmail_ID()

  {
        Microsoft.Office.Interop.Outlook._NameSpace ns = null;
        // Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
        Outlook._Application oApp = new Outlook.Application();
        Object selObject = oApp.ActiveExplorer().Selection[1];
        Microsoft.Office.Interop.Outlook.Application app = null;
        app = new Microsoft.Office.Interop.Outlook.Application();
        LogWriter.LogWrite1("Application :");
        ns = app.GetNamespace("MAPI");
        Outlook.MailItem mailItem = (selObject as Outlook.MailItem);
        createdbyEmail = mailItem.UserProperties.Session.CurrentUser.Address;

        if (app.Session.CurrentUser.AddressEntry.Type == "EX")
        {
            createdbyEmail = app.Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
        }
        else
        {
            createdbyEmail = app.Session.CurrentUser.AddressEntry.Address;
        }

        return createdbyEmail;
    }
tripleee
  • 175,061
  • 34
  • 275
  • 318