Showing posts with label get all account details from outlook programatically. Show all posts
Showing posts with label get all account details from outlook programatically. Show all posts

Tuesday, October 18, 2011

Get Information About Multiple Accounts in outlook


//namespace to use (add microsoft.office.interop.outlook dll )
using Outlook = Microsoft.Office.Interop.Outlook;
private void getallAccountsInOutlook()
{
    Outlook.Accounts accounts = Application.Session.Accounts;
    foreach (Outlook.Account account in accounts)
    {
        try
        {
            StringBuilder allInfoInThisString = new StringBuilder();
            allInfoInThisString.AppendLine("Account: " + account.DisplayName);
            if (string.IsNullOrEmpty(account.SmtpAddress)
                || string.IsNullOrEmpty(account.UserName))
            {
                Outlook.AddressEntry addressEntryObject =account.CurrentUser.AddressEntry as Outlook.AddressEntry;
                if (addressEntryObject.Type == "EX")
                {
                    Outlook.ExchangeUser oEU =
                        addressEntryObject.GetExchangeUser()
                        as Outlook.ExchangeUser;
                    allInfoInThisString.AppendLine("UserName: " +
                        oEU.Name);
                    allInfoInThisString.AppendLine("SMTP: " +
                        oEU.PrimarySmtpAddress);
                    allInfoInThisString.AppendLine("Exchange Server: " +
                        account.ExchangeMailboxServerName);
                    allInfoInThisString.AppendLine("Exchange Server Version: " +
                        account.ExchangeMailboxServerVersion); 
                }
                else
                {
                    allInfoInThisString.AppendLine("UserName: " +
                        addressEntryObject.Name);
                    allInfoInThisString.AppendLine("SMTP: " +
                        addressEntryObject.Address);
                }
            }
            else
            {
                allInfoInThisString.AppendLine("UserName: " +
                    account.UserName);
                allInfoInThisString.AppendLine("SMTP: " +
                    account.SmtpAddress);
                if(account.AccountType == 
                    Outlook.OlAccountType.olExchange)
                {
                    allInfoInThisString.AppendLine("Exchange Server: " +
                        account.ExchangeMailboxServerName);
                    allInfoInThisString.AppendLine("Exchange Server Version: " +
                        account.ExchangeMailboxServerVersion); 
                }
            }
            if(account.DeliveryStore !=null)
            {
                allInfoInThisString.AppendLine("Delivery Store: " +
                    account.DeliveryStore.DisplayName);
            }
            allInfoInThisString.AppendLine("_________________________");
            Debug.Write(allInfoInThisString.ToString());
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }
}

Featured Posts

Kali Linux Remote Desktop: Access GNOME from Windows Using Native RDP

  Kali Linux + GNOME 50 + GNOME Remote Desktop + Windows Remote Desktop (MSTSC) Getting a full GNOME desktop remotely on Kali Linux can be ...