Esempio n. 1
0
 private static void CreateOutlookNamespace()
 {
     //Try to create new Outlook namespace 5 times, because mostly it fails the first time, if not yet running
     for (int i = 0; i < 5; i++)
     {
         try
         {
             _outlookNamespace = OutlookApplication.GetNamespace("MAPI");
             break;  //Exit the for loop, if getting outlook namespace was successful
         }
         catch (COMException ex)
         {
             if (ex.ErrorCode == unchecked ((int)0x80029c4a))
             {
                 Logger.Log(ex, EventType.Debug);
                 throw new NotSupportedException(OutlookRegistryUtils.GetPossibleErrorDiagnosis(), ex);
             }
             if (i == 4)
             {
                 Logger.Log(ex, EventType.Debug);
                 throw new NotSupportedException("Could not connect to 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and running.", ex);
             }
             else
             {
                 Logger.Log("Try: " + i, EventType.Debug);
                 Thread.Sleep(1000 * 10 * (i + 1));
             }
         }
         catch (InvalidCastException ex)
         {
             Logger.Log(ex, EventType.Debug);
             throw new NotSupportedException(OutlookRegistryUtils.GetPossibleErrorDiagnosis(), ex);
         }
         catch (Exception ex)
         {
             if (i == 4)
             {
                 Logger.Log(ex, EventType.Debug);
                 throw new NotSupportedException("Could not connect to 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and running.", ex);
             }
             else
             {
                 Logger.Log("Try: " + i, EventType.Debug);
                 Thread.Sleep(1000 * 10 * (i + 1));
             }
         }
     }
 }
Esempio n. 2
0
        private static void CreateOutlookInstance()
        {
            if (OutlookApplication == null || _outlookNamespace == null)
            {
                CreateOutlookApplication();

                if (OutlookApplication == null)
                {
                    throw new NotSupportedException("Could not create instance of 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and retry.");
                }

                CreateOutlookNamespace();

                if (_outlookNamespace == null)
                {
                    throw new NotSupportedException("Could not connect to 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and retry.");
                }
                else
                {
                    Logger.Log("Connected to Outlook: " + VersionInformation.GetOutlookVersion(OutlookApplication), EventType.Debug);
                }
            }

            //Just try to access the outlookNamespace to check, if it is still accessible, throws COMException, if not reachable
            try
            {
                if (string.IsNullOrEmpty(ContactsSynchronizer.SyncContactsFolder))
                {
                    _outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
                }
                else
                {
                    _outlookNamespace.GetFolderFromID(ContactsSynchronizer.SyncContactsFolder);
                }
            }
            catch (COMException ex)
            {
                if (ex.ErrorCode == unchecked ((int)0x80029c4a))
                {
                    Logger.Log(ex, EventType.Debug);
                    throw new NotSupportedException(OutlookRegistryUtils.GetPossibleErrorDiagnosis(), ex);
                }
                else if (ex.ErrorCode == unchecked ((int)0x80040111)) //"The server is not available. Contact your administrator if this condition persists."
                {
                    try
                    {
                        Logger.Log("Trying to logon, 1st try", EventType.Debug);
                        _outlookNamespace.Logon("", "", false, false);
                        Logger.Log("1st try OK", EventType.Debug);
                    }
                    catch (Exception e1)
                    {
                        Logger.Log(e1, EventType.Debug);
                        try
                        {
                            Logger.Log("Trying to logon, 2nd try", EventType.Debug);
                            _outlookNamespace.Logon("", "", true, true);
                            Logger.Log("2nd try OK", EventType.Debug);
                        }
                        catch (Exception e2)
                        {
                            Logger.Log(e2, EventType.Debug);
                            throw new NotSupportedException("Could not connect to 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and running.", e2);
                        }
                    }
                }
                else
                {
                    Logger.Log(ex, EventType.Debug);
                    throw new NotSupportedException("Could not connect to 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and running.", ex);
                }
            }
        }
Esempio n. 3
0
 private static void CreateOutlookApplication()
 {
     //Try to create new Outlook application 3 times, because mostly it fails the first time, if not yet running
     for (int i = 0; i < 3; i++)
     {
         try
         {
             // First try to get the running application in case Outlook is already started
             try
             {
                 OutlookApplication = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
                 break;  //Exit the for loop, if creating outlook application was successful
             }
             catch (COMException ex)
             {
                 if (ex.ErrorCode == unchecked ((int)0x80029c4a))
                 {
                     Logger.Log(ex, EventType.Debug);
                     throw new NotSupportedException(OutlookRegistryUtils.GetPossibleErrorDiagnosis(), ex);
                 }
                 // That failed - try to create a new application object, launching Outlook in the background
                 OutlookApplication = new Outlook.Application();
                 break;
             }
             catch (InvalidCastException ex)
             {
                 Logger.Log(ex, EventType.Debug);
                 throw new NotSupportedException(OutlookRegistryUtils.GetPossibleErrorDiagnosis(), ex);
             }
             catch (Exception ex)
             {
                 if (i == 2)
                 {
                     Logger.Log(ex, EventType.Debug);
                     throw new NotSupportedException("Could not connect to 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and running.", ex);
                 }
                 else
                 {
                     Thread.Sleep(1000 * 10 * (i + 1));
                 }
             }
         }
         catch (COMException ex)
         {
             if (ex.ErrorCode == unchecked ((int)0x80029c4a))
             {
                 Logger.Log(ex, EventType.Debug);
                 throw new NotSupportedException(OutlookRegistryUtils.GetPossibleErrorDiagnosis(), ex);
             }
             if (i == 2)
             {
                 Logger.Log(ex, EventType.Debug);
                 throw new NotSupportedException("Could not create instance of 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and retry.", ex);
             }
             else
             {
                 Thread.Sleep(1000 * 10 * (i + 1));
             }
         }
         catch (InvalidCastException ex)
         {
             Logger.Log(ex, EventType.Debug);
             throw new NotSupportedException(OutlookRegistryUtils.GetPossibleErrorDiagnosis(), ex);
         }
         catch (Exception ex)
         {
             if (i == 2)
             {
                 Logger.Log(ex, EventType.Debug);
                 throw new NotSupportedException("Could not create instance of 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and retry.", ex);
             }
             else
             {
                 Thread.Sleep(1000 * 10 * (i + 1));
             }
         }
     }
 }