WebHosting

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);
        }
    }
}

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

How to Enable and Configure Remote Desktop (RDP) on Ubuntu 24.04.4 LTS

Remote Desktop Protocol (RDP) support in Ubuntu has become considerably easier with the GNOME Remote Desktop stack included in modern Ubuntu...