Search code examples
c#outlookadd-inimapexchange-server

Outlook Add-in read Server Settings


I am searching for a way to read out IMAP/Microsoft Exchange settings from Outlook with an Outlook Add-In coded in c#.

Thanks for helping me.


Solution

  • I think what you're looking for is the Accounts interface of the Office.Interop.Outlook dll.

    You haven't mentioned what version of Outlook you're using, this code is for Outlook 2010, written by Microsoft MVP Helmut Obertanner, taken from here :

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

    I can see from the documentation that this should in theory work for Outlook 2007 as well.

    If you intend to use Outlook 2003, you're going to have to do things a bit differently as the Outlook 2003 object model did not include access to the Accounts property.

    To do it, you'll either have to use a third party library such as redemption for example, see this answer for some alternatives.

    You can apparently also use the Registry to do it as per this answer to another question.