Example #1
0
        private void CreateGroups()
        {
            // Go through all the mailboxes and organise into groups based on grouping information
            _groups = new Dictionary <string, GroupInfo>();  // Clear any existing groups
            string autodiscoverUrl = "";

            if (radioButtonOffice365.Checked)
            {
                autodiscoverUrl = "https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc";
            }
            _mailboxes.GroupMailboxes    = !radioButtonSpecificUri.Checked;
            _mailboxes.CredentialHandler = CredentialHandler();
            //Auth.CredentialHandler credentialHandler = CredentialHandler();

            foreach (string sMailbox in checkedListBoxMailboxes.CheckedItems)
            {
                _mailboxes.AddMailbox(sMailbox, autodiscoverUrl);
                MailboxInfo mailboxInfo = _mailboxes.Mailbox(sMailbox);
                if (mailboxInfo != null)
                {
                    GroupInfo groupInfo = null;
                    if (_groups.ContainsKey(mailboxInfo.GroupName))
                    {
                        groupInfo = _groups[mailboxInfo.GroupName];
                    }
                    else
                    {
                        groupInfo = new GroupInfo(mailboxInfo.GroupName, mailboxInfo.SMTPAddress, mailboxInfo.EwsUrl, _credentialHandler, _traceListener);
                        _groups.Add(mailboxInfo.GroupName, groupInfo);
                    }
                    if (groupInfo.Mailboxes.Count > 199)
                    {
                        // We already have enough mailboxes in this group, so we rename it and create a new one
                        // Renaming it means that we can still put new mailboxes into the correct group based on GroupingInformation
                        int i = 1;
                        while (_groups.ContainsKey(String.Format("{0}{1}", groupInfo.Name, i)))
                        {
                            i++;
                        }
                        _groups.Remove(groupInfo.Name);
                        _groups.Add(String.Format("{0}{1}", groupInfo.Name, i), groupInfo);
                        groupInfo = new GroupInfo(mailboxInfo.GroupName, mailboxInfo.SMTPAddress, mailboxInfo.EwsUrl, _credentialHandler, _traceListener);
                        _groups.Add(mailboxInfo.GroupName, groupInfo);
                    }

                    groupInfo.Mailboxes.Add(sMailbox);
                }
            }
        }
Example #2
0
        private ExchangeService ExchangeServiceForMailboxAccess(MailboxInfo Mailbox)
        {
            // Create an ExchangeService object for mailbox access (used to retrieve further information about notified items)

            ExchangeService mailboxAccessService = new ExchangeService(ExchangeVersion.Exchange2016);

            CredentialHandler().ApplyCredentialsToExchangeService(mailboxAccessService);
            mailboxAccessService.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, Mailbox.SMTPAddress);
            mailboxAccessService.HttpHeaders.Add("X-AnchorMailbox", Mailbox.SMTPAddress);
            mailboxAccessService.HttpHeaders.Add("client-request-id", Guid.NewGuid().ToString());
            mailboxAccessService.HttpHeaders.Add("return-client-request-id", "true");
            mailboxAccessService.Url           = new Uri(Mailbox.EwsUrl);
            mailboxAccessService.TraceListener = _traceListener;
            mailboxAccessService.TraceFlags    = TraceFlags.All;
            mailboxAccessService.TraceEnabled  = true;
            return(mailboxAccessService);
        }
Example #3
0
        public bool AddMailbox(string SMTPAddress)
        {
            // Perform autodiscover for the mailbox and store the information

            if (_mailboxes.ContainsKey(SMTPAddress))
            {
                // We already have autodiscover information for this mailbox, if it is recent enough we don't bother retrieving it again
                if (_mailboxes[SMTPAddress].IsStale)
                {
                    _mailboxes.Remove(SMTPAddress);
                }
                else
                {
                    return(true);
                }
            }

            // Retrieve the autodiscover information
            GetUserSettingsResponse userSettings = null;

            try
            {
                userSettings = GetUserSettings(SMTPAddress);
            }
            catch (Exception ex)
            {
                _logger.Log(String.Format("Failed to autodiscover for {0}: {1}", SMTPAddress, ex.Message));
                return(false);
            }

            // Store the autodiscover result, and check that we have what we need for subscriptions
            MailboxInfo info = new MailboxInfo(SMTPAddress, userSettings);

            if (!info.HaveSubscriptionInformation)
            {
                _logger.Log(String.Format("Autodiscover succeeded, but EWS Url was not returned for {0}", SMTPAddress));
                return(false);
            }

            // Add the mailbox to our list, and if it will be part of a new group add that to the group list (with this mailbox as the primary mailbox)
            _mailboxes.Add(info.SMTPAddress, info);
            return(true);
        }
Example #4
0
        private void CreateGroups()
        {
            // Go through all the mailboxes and organise into groups based on grouping information
            _groups = new Dictionary <string, GroupInfo>();  // Clear any existing groups
            _mailboxes.Credentials = new WebCredentials(textBoxUsername.Text, textBoxPassword.Text);

            foreach (string sMailbox in checkedListBoxMailboxes.CheckedItems)
            {
                _mailboxes.AddMailbox(sMailbox);
                MailboxInfo mailboxInfo = _mailboxes.Mailbox(sMailbox);
                if (mailboxInfo != null)
                {
                    GroupInfo groupInfo = null;
                    if (_groups.ContainsKey(mailboxInfo.GroupName))
                    {
                        groupInfo = _groups[mailboxInfo.GroupName];
                    }
                    else
                    {
                        groupInfo = new GroupInfo(mailboxInfo.GroupName, mailboxInfo.SMTPAddress, mailboxInfo.EwsUrl, _traceListener);
                        _groups.Add(mailboxInfo.GroupName, groupInfo);
                    }
                    if (groupInfo.Mailboxes.Count > 199)
                    {
                        // We already have enough mailboxes in this group, so we rename it and create a new one
                        // Renaming it means that we can still put new mailboxes into the correct group based on GroupingInformation
                        int i = 1;
                        while (_groups.ContainsKey(String.Format("{0}{1}", groupInfo.Name, i)))
                        {
                            i++;
                        }
                        _groups.Remove(groupInfo.Name);
                        _groups.Add(String.Format("{0}{1}", groupInfo.Name, i), groupInfo);
                        groupInfo = new GroupInfo(mailboxInfo.GroupName, mailboxInfo.SMTPAddress, mailboxInfo.EwsUrl, _traceListener);
                        _groups.Add(mailboxInfo.GroupName, groupInfo);
                    }

                    groupInfo.Mailboxes.Add(sMailbox);
                }
            }
        }
        public bool AddMailbox(string SMTPAddress, string AutodiscoverURL = "")
        {
            // Perform autodiscover for the mailbox and store the information

            if (_mailboxes.ContainsKey(SMTPAddress))
            {
                // We already have autodiscover information for this mailbox, if it is recent enough we don't bother retrieving it again
                if (_mailboxes[SMTPAddress].IsStale)
                {
                    _mailboxes.Remove(SMTPAddress);
                }
                else
                {
                    return(true);
                }
            }

            MailboxInfo info = null;

            // Retrieve the autodiscover information
            _logger.Log($"Retrieving user settings for {SMTPAddress}");
            GetUserSettingsResponse userSettings = null;

            if (!String.IsNullOrEmpty(AutodiscoverURL))
            {
                // Use the supplied Autodiscover URL
                try
                {
                    _autodiscover.Url = new Uri(AutodiscoverURL);
                    userSettings      = _autodiscover.GetUserSettings(SMTPAddress, UserSettingName.InternalEwsUrl, UserSettingName.ExternalEwsUrl, UserSettingName.GroupingInformation);
                }
                catch
                {
                }
            }

            if (userSettings == null)
            {
                try
                {
                    // Try full autodiscover
                    userSettings = GetUserSettings(SMTPAddress);
                }
                catch (Exception ex)
                {
                    _logger.Log(String.Format("Failed to autodiscover for {0}: {1}", SMTPAddress, ex.Message));
                    return(false);
                }
            }

            // Store the autodiscover result, and check that we have what we need for subscriptions
            info = new MailboxInfo(SMTPAddress, userSettings);


            if (!info.HaveSubscriptionInformation)
            {
                _logger.Log(String.Format("Autodiscover succeeded, but EWS Url was not returned for {0}", SMTPAddress));
                return(false);
            }

            // Add the mailbox to our list, and if it will be part of a new group add that to the group list (with this mailbox as the primary mailbox)
            _mailboxes.Add(info.SMTPAddress, info);
            return(true);
        }