예제 #1
0
        public void Connect()
        {
            oApp = OutlookCalendar.AttachToOutlook();
            log.Debug("Setting up Outlook connection.");

            // Get the NameSpace and Logon information.
            NameSpace oNS = oApp.GetNamespace("mapi");

            //Log on by using a dialog box to choose the profile.
            //oNS.Logon("", Type.Missing, true, true);

            //Implicit logon to default profile, with no dialog box
            //If 1< profile, a dialogue is forced unless implicit login used
            exchangeConnectionMode = oNS.ExchangeConnectionMode;
            if (exchangeConnectionMode != OlExchangeConnectionMode.olNoExchange)
            {
                log.Info("Exchange server version: " + oNS.ExchangeMailboxServerVersion.ToString());
            }

            //Logon using a specific profile. Can't see a use case for this when using OGsync
            //If using this logon method, change the profile name to an appropriate value:
            //HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles
            //oNS.Logon("YourValidProfile", Type.Missing, false, true);

            log.Info("Exchange connection mode: " + exchangeConnectionMode.ToString());
            currentUserSMTP = GetRecipientEmail(oNS.CurrentUser);
            currentUserName = oNS.CurrentUser.Name;
            if (currentUserName == "Unknown")
            {
                log.Info("Current username is \"Unknown\"");
                if (Settings.Instance.AddAttendees)
                {
                    System.Windows.Forms.MessageBox.Show("It appears you do not have an Email Account configured in Outlook.\r\n" +
                                                         "You should set one up now (Tools > Email Accounts) to avoid problems syncing meeting attendees.",
                                                         "No Email Account Found", System.Windows.Forms.MessageBoxButtons.OK,
                                                         System.Windows.Forms.MessageBoxIcon.Warning);
                }
            }

            //Get the accounts configured in Outlook
            accounts = oNS.Accounts;

            // Get the Calendar folders
            useOutlookCalendar = getDefaultCalendar(oNS);
            if (MainForm.Instance.IsHandleCreated)   //resetting connection, so pick up selected calendar from GUI dropdown
            //***This might be cross thread, so don't rely on MainForm
            {
                MainForm.Instance.cbOutlookCalendars.DataSource = new BindingSource(calendarFolders, null);
                KeyValuePair <String, MAPIFolder> calendar = (KeyValuePair <String, MAPIFolder>)MainForm.Instance.cbOutlookCalendars.SelectedItem;
                calendar           = (KeyValuePair <String, MAPIFolder>)MainForm.Instance.cbOutlookCalendars.SelectedItem;
                useOutlookCalendar = calendar.Value;
            }

            // Done. Log off.
            oNS.Logoff();
        }
예제 #2
0
        public void Connect()
        {
            oApp = OutlookCalendar.AttachToOutlook();
            log.Debug("Setting up Outlook connection.");

            // Get the NameSpace and Logon information.
            NameSpace oNS = oApp.GetNamespace("mapi");

            //Implicit logon to default profile, with no dialog box
            //If 1< profile, a dialogue is forced unless implicit login used
            exchangeConnectionMode = oNS.ExchangeConnectionMode;
            if (exchangeConnectionMode != OlExchangeConnectionMode.olNoExchange)
            {
                log.Info("Exchange server version: Unknown");
            }
            log.Info("Exchange connection mode: " + exchangeConnectionMode.ToString());

            currentUserSMTP = GetRecipientEmail(oNS.CurrentUser);
            currentUserName = oNS.CurrentUser.Name;
            if (currentUserName == "Unknown")
            {
                log.Info("Current username is \"Unknown\"");
                if (Settings.Instance.AddAttendees)
                {
                    System.Windows.Forms.MessageBox.Show("It appears you do not have an Email Account configured in Outlook.\r\n" +
                                                         "You should set one up now (Tools > Email Accounts) to avoid problems syncing meeting attendees.",
                                                         "No Email Account Found", System.Windows.Forms.MessageBoxButtons.OK,
                                                         System.Windows.Forms.MessageBoxIcon.Warning);
                }
            }

            // Get the Calendar folders
            useOutlookCalendar = getDefaultCalendar(oNS);
            if (MainForm.Instance.IsHandleCreated)   //resetting connection, so pick up selected calendar from GUI dropdown
            {
                MainForm.Instance.cbOutlookCalendars.DataSource = new BindingSource(calendarFolders, null);
                KeyValuePair <String, MAPIFolder> calendar = (KeyValuePair <String, MAPIFolder>)MainForm.Instance.cbOutlookCalendars.SelectedItem;
                calendar           = (KeyValuePair <String, MAPIFolder>)MainForm.Instance.cbOutlookCalendars.SelectedItem;
                useOutlookCalendar = calendar.Value;
            }

            // Done. Log off.
            oNS.Logoff();
        }
예제 #3
0
        private static void getOutlookVersion()
        {
            Microsoft.Office.Interop.Outlook.Application oApp = OutlookCalendar.AttachToOutlook();
            outlookVersionFull = oApp.Version;

            /* try {
             *  outlookVersionFull = oApp.Version;
             * } catch (System.Exception ex) {
             *  if (ex.Message.Contains("RPC_E_CALL_REJECTED")) { //Issue #86: Call was rejected by callee.
             *      log.Warn("The Outlook GUI is not quite ready yet - waiting until it is...");
             *      int maxWait = 30;
             *      while (maxWait > 0) {
             *          if (maxWait < 30 && maxWait % 10 == 0) { log.Debug("Still waiting..."); }
             *          try {
             *              outlookVersionFull = oApp.Version;
             *              maxWait = 0;
             *          } catch { }
             *          System.Threading.Thread.Sleep(1000);
             *          maxWait--;
             *      }
             *      if (maxWait > 0) {
             *          log.Error("Given up waiting for Outlook to respond.");
             *          throw new ApplicationException("Outlook would not give timely response.");
             *      }
             *  } else {
             *      throw ex;
             *  }
             * } finally {
             *  System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oApp);
             *  oApp = null;
             * }*/

            log.Info("Outlook Version: " + outlookVersionFull);
            if (testing2003)
            {
                log.Info("*** 2003 TESTING ***");
                outlookVersionFull = "11";
            }
            outlookVersion = Convert.ToInt16(outlookVersionFull.Split(Convert.ToChar("."))[0]);
        }
예제 #4
0
        private static void getOutlookVersion()
        {
            //Attach just to get Outlook version - we don't know whether to provide New or Old interface yet
            Microsoft.Office.Interop.Outlook.Application oApp = null;
            try {
                OutlookCalendar.AttachToOutlook(ref oApp);
                outlookVersionFull = oApp.Version;

                log.Info("Outlook Version: " + outlookVersionFull);
                if (testing2003)
                {
                    log.Info("*** 2003 TESTING ***");
                    outlookVersionFull = "11";
                }
                outlookVersion = Convert.ToInt16(outlookVersionFull.Split(Convert.ToChar("."))[0]);
            } finally {
                if (oApp != null)
                {
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oApp);
                    oApp = null;
                }
            }
        }
예제 #5
0
        public void Connect()
        {
            OutlookCalendar.AttachToOutlook(ref oApp, openOutlookOnFail: true, withSystemCall: false);
            log.Debug("Setting up Outlook connection.");

            // Get the NameSpace and Logon information.
            NameSpace oNS = null;

            try {
                oNS = oApp.GetNamespace("mapi");

                //Log on by using a dialog box to choose the profile.
                //oNS.Logon("", Type.Missing, true, true);

                //Implicit logon to default profile, with no dialog box
                //If 1< profile, a dialogue is forced unless implicit login used
                exchangeConnectionMode = oNS.ExchangeConnectionMode;
                if (exchangeConnectionMode != OlExchangeConnectionMode.olNoExchange)
                {
                    log.Info("Exchange server version: " + oNS.ExchangeMailboxServerVersion.ToString());
                }

                //Logon using a specific profile. Can't see a use case for this when using OGsync
                //If using this logon method, change the profile name to an appropriate value:
                //HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles
                //oNS.Logon("YourValidProfile", Type.Missing, false, true);

                log.Info("Exchange connection mode: " + exchangeConnectionMode.ToString());

                Recipient currentUser = null;
                try {
                    try {
                        currentUser = oNS.CurrentUser;
                    } catch {
                        log.Warn("We seem to have a faux connection to Outlook! Forcing starting it with a system call :-/");
                        oNS = (NameSpace)OutlookCalendar.ReleaseObject(oNS);
                        Disconnect();
                        OutlookCalendar.AttachToOutlook(ref oApp, openOutlookOnFail: true, withSystemCall: true);
                        oNS         = oApp.GetNamespace("mapi");
                        currentUser = oNS.CurrentUser;
                    }

                    //Issue 402
                    log.Debug("Getting active window inspector");
                    Inspector inspector = oApp.ActiveInspector();
                    inspector = (Inspector)OutlookCalendar.ReleaseObject(inspector);
                    log.Debug("Done.");
                    currentUserSMTP = GetRecipientEmail(currentUser);
                    currentUserName = currentUser.Name;
                } finally {
                    currentUser = (Recipient)OutlookCalendar.ReleaseObject(currentUser);
                }

                if (currentUserName == "Unknown")
                {
                    log.Info("Current username is \"Unknown\"");
                    if (Settings.Instance.AddAttendees)
                    {
                        System.Windows.Forms.MessageBox.Show("It appears you do not have an Email Account configured in Outlook.\r\n" +
                                                             "You should set one up now (Tools > Email Accounts) to avoid problems syncing meeting attendees.",
                                                             "No Email Account Found", System.Windows.Forms.MessageBoxButtons.OK,
                                                             System.Windows.Forms.MessageBoxIcon.Warning);
                    }
                }

                //Get the folders configured in Outlook
                folders = oNS.Folders;

                // Get the Calendar folders
                useOutlookCalendar = getCalendarStore(oNS);
                if (MainForm.Instance.IsHandleCreated)
                {
                    log.Fine("Resetting connection, so re-selecting calendar from GUI dropdown");

                    MainForm.Instance.cbOutlookCalendars.SelectedIndexChanged -= MainForm.Instance.cbOutlookCalendar_SelectedIndexChanged;
                    MainForm.Instance.cbOutlookCalendars.DataSource            = new BindingSource(calendarFolders, null);

                    //Select the right calendar
                    int c = 0;
                    foreach (KeyValuePair <String, MAPIFolder> calendarFolder in calendarFolders)
                    {
                        if (calendarFolder.Value.EntryID == Settings.Instance.UseOutlookCalendar.Id)
                        {
                            MainForm.Instance.SetControlPropertyThreadSafe(MainForm.Instance.cbOutlookCalendars, "SelectedIndex", c);
                        }
                        c++;
                    }
                    if ((int)MainForm.Instance.GetControlPropertyThreadSafe(MainForm.Instance.cbOutlookCalendars, "SelectedIndex") == -1)
                    {
                        MainForm.Instance.SetControlPropertyThreadSafe(MainForm.Instance.cbOutlookCalendars, "SelectedIndex", 0);
                    }

                    KeyValuePair <String, MAPIFolder> calendar = (KeyValuePair <String, MAPIFolder>)MainForm.Instance.GetControlPropertyThreadSafe(MainForm.Instance.cbOutlookCalendars, "SelectedItem");
                    useOutlookCalendar = calendar.Value;

                    MainForm.Instance.cbOutlookCalendars.SelectedIndexChanged += MainForm.Instance.cbOutlookCalendar_SelectedIndexChanged;
                }
            } finally {
                // Done. Log off.
                oNS.Logoff();
                oNS = (NameSpace)OutlookCalendar.ReleaseObject(oNS);
            }
        }
예제 #6
0
        public void Connect()
        {
            OutlookCalendar.AttachToOutlook(ref oApp, openOutlookOnFail: true, withSystemCall: false);
            log.Debug("Setting up Outlook connection.");

            // Get the NameSpace and Logon information.
            NameSpace oNS = null;

            try {
                oNS = oApp.GetNamespace("mapi");

                //Implicit logon to default profile, with no dialog box
                //If 1< profile, a dialogue is forced unless implicit login used
                exchangeConnectionMode = oNS.ExchangeConnectionMode;
                if (exchangeConnectionMode != OlExchangeConnectionMode.olNoExchange)
                {
                    log.Info("Exchange server version: Unknown");
                }
                log.Info("Exchange connection mode: " + exchangeConnectionMode.ToString());

                oNS = GetCurrentUser(oNS);

                if (!Settings.Instance.OutlookGalBlocked && currentUserName == "Unknown")
                {
                    log.Info("Current username is \"Unknown\"");
                    if (Settings.Instance.AddAttendees)
                    {
                        System.Windows.Forms.MessageBox.Show("It appears you do not have an Email Account configured in Outlook.\r\n" +
                                                             "You should set one up now (Tools > Email Accounts) to avoid problems syncing meeting attendees.",
                                                             "No Email Account Found", System.Windows.Forms.MessageBoxButtons.OK,
                                                             System.Windows.Forms.MessageBoxIcon.Warning);
                    }
                }

                log.Debug("Get the folders configured in Outlook");
                folders = oNS.Folders;

                // Get the Calendar folders
                useOutlookCalendar = getCalendarStore(oNS);
                if (MainForm.Instance.IsHandleCreated)
                {
                    log.Fine("Resetting connection, so re-selecting calendar from GUI dropdown");

                    MainForm.Instance.cbOutlookCalendars.SelectedIndexChanged -= MainForm.Instance.cbOutlookCalendar_SelectedIndexChanged;
                    MainForm.Instance.cbOutlookCalendars.DataSource            = new BindingSource(calendarFolders, null);

                    //Select the right calendar
                    int c = 0;
                    foreach (KeyValuePair <String, MAPIFolder> calendarFolder in calendarFolders)
                    {
                        if (calendarFolder.Value.EntryID == Settings.Instance.UseOutlookCalendar.Id)
                        {
                            MainForm.Instance.SetControlPropertyThreadSafe(MainForm.Instance.cbOutlookCalendars, "SelectedIndex", c);
                        }
                        c++;
                    }
                    if ((int)MainForm.Instance.GetControlPropertyThreadSafe(MainForm.Instance.cbOutlookCalendars, "SelectedIndex") == -1)
                    {
                        MainForm.Instance.SetControlPropertyThreadSafe(MainForm.Instance.cbOutlookCalendars, "SelectedIndex", 0);
                    }

                    KeyValuePair <String, MAPIFolder> calendar = (KeyValuePair <String, MAPIFolder>)MainForm.Instance.GetControlPropertyThreadSafe(MainForm.Instance.cbOutlookCalendars, "SelectedItem");
                    useOutlookCalendar = calendar.Value;

                    MainForm.Instance.cbOutlookCalendars.SelectedIndexChanged += MainForm.Instance.cbOutlookCalendar_SelectedIndexChanged;
                }
            } finally {
                // Done. Log off.
                if (oNS != null)
                {
                    oNS.Logoff();
                }
                oNS = (NameSpace)OutlookCalendar.ReleaseObject(oNS);
            }
        }
예제 #7
0
        public NameSpace GetCurrentUser(NameSpace oNS)
        {
            //We only need the current user details when syncing meeting attendees.
            //If GAL had previously been detected as blocked, let's always try one attempt to see if it's been opened up
            if (!Settings.Instance.OutlookGalBlocked && !Settings.Instance.AddAttendees)
            {
                return(oNS);
            }

            Boolean releaseNamespace = (oNS == null);

            if (releaseNamespace)
            {
                oNS = oApp.GetNamespace("mapi");
            }

            Recipient currentUser = null;

            try {
                DateTime triggerOOMsecurity = DateTime.Now;
                try {
                    currentUser = oNS.CurrentUser;
                    if (!MainForm.Instance.IsHandleCreated && (DateTime.Now - triggerOOMsecurity).TotalSeconds > 1)
                    {
                        log.Warn(">1s delay possibly due to Outlook security popup.");
                        OutlookCalendar.OOMsecurityInfo = true;
                    }
                } catch (System.Exception ex) {
                    OGCSexception.Analyse(ex);
                    if (Settings.Instance.OutlookGalBlocked)   //Fail fast
                    {
                        log.Debug("Corporate policy is still blocking access to GAL.");
                        return(oNS);
                    }
                    log.Warn("We seem to have a faux connection to Outlook! Forcing starting it with a system call :-/");
                    oNS = (NameSpace)OutlookCalendar.ReleaseObject(oNS);
                    Disconnect();
                    OutlookCalendar.AttachToOutlook(ref oApp, openOutlookOnFail: true, withSystemCall: true);
                    oNS = oApp.GetNamespace("mapi");

                    int maxDelay = 5;
                    int delay    = 1;
                    while (delay <= maxDelay)
                    {
                        log.Debug("Sleeping..." + delay + "/" + maxDelay);
                        System.Threading.Thread.Sleep(10000);
                        try {
                            currentUser = oNS.CurrentUser;
                            delay       = maxDelay;
                        } catch (System.Exception ex2) {
                            if (delay == maxDelay)
                            {
                                if (OGCSexception.GetErrorCode(ex2) == "0x80004004")   //E_ABORT
                                {
                                    log.Warn("Corporate policy or possibly anti-virus is blocking access to GAL.");
                                }
                                else
                                {
                                    OGCSexception.Analyse(ex2);
                                }
                                log.Warn("OGCS is unable to obtain CurrentUser from Outlook.");
                                Settings.Instance.OutlookGalBlocked = true;
                                return(oNS);
                            }
                            OGCSexception.Analyse(ex2);
                        }
                        delay++;
                    }
                }
                if (Settings.Instance.OutlookGalBlocked)
                {
                    log.Debug("GAL is no longer blocked!");
                }
                Settings.Instance.OutlookGalBlocked = false;
                currentUserSMTP = GetRecipientEmail(currentUser);
                currentUserName = currentUser.Name;
            } finally {
                currentUser = (Recipient)OutlookCalendar.ReleaseObject(currentUser);
                if (releaseNamespace)
                {
                    oNS = (NameSpace)OutlookCalendar.ReleaseObject(oNS);
                }
            }
            return(oNS);
        }