Exemplo n.º 1
0
        public async Task <bool> ConnectExchange(string authToken)
        {
            bool success = true;

            try
            {
                ThisExchangeService = new ExchangeService
                {
                    Url         = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"),
                    Credentials = new OAuthCredentials(authToken)
                };

                Folder          Inbox         = Folder.Bind(ThisExchangeService, WellKnownFolderName.Inbox);
                AlternateId     aiAlternateid = new AlternateId(IdFormat.EwsId, Inbox.Id.UniqueId, "*****@*****.**");
                AlternateIdBase aiResponse    = ThisExchangeService.ConvertId(aiAlternateid, IdFormat.EwsId);
                ThisMailbox = ((AlternateId)aiResponse).Mailbox;
            }
            catch (Exception ex)
            {
                success = false;
                Logger.FireWindowsLog(WinLogger.ApplicationEventType.Error, "Exchange Operation Failed - " + ex);
            }

            //success = await EnumerateFoldersAsync();
            return(success);
        }
Exemplo n.º 2
0
        private bool SubscribeForNotifications()
        {
            try
            {
                AlternateId outlookFolderId = new AlternateId(IdFormat.HexEntryId, _folder.EntryID, _primarySmtpAddress, false);
                AlternateId ewsFolderId     = _exchangeService.ConvertId(outlookFolderId, IdFormat.EwsId) as AlternateId;
                _folderId = new FolderId(ewsFolderId.UniqueId);
            }
            catch (Exception ex)
            {
                AddLog(String.Format("Failed to obtain EWS Folder Id: {0}", ex.Message));
                if (ex.Message.Contains("Unauthorized") || ex.Message.Contains("401"))
                {
                    AddLog("Currently only Windows auth will work (on-prem only)");
                }
                return(false);
            }

            try
            {
                _streamingSubscription = _exchangeService.SubscribeToStreamingNotifications(new FolderId[] { _folderId }, EventType.Created, EventType.Moved, EventType.Copied, EventType.Modified, EventType.NewMail, EventType.Deleted);
                // If we have a watermark, we set this so that we don't miss any events
            }
            catch (Exception ex)
            {
                AddLog(String.Format("Error creating subscription: {0}", ex.Message));
                return(false);
            }

            try
            {
                _streamingSubscriptionConnection = new StreamingSubscriptionConnection(_exchangeService, 30);
                _streamingSubscriptionConnection.AddSubscription(_streamingSubscription);
            }
            catch (Exception ex)
            {
                AddLog(String.Format("Error creating subscription connection: {0}", ex.Message));
                return(false);
            }

            _streamingSubscriptionConnection.OnNotificationEvent += _streamingSubscriptionConnection_OnNotificationEvent;
            _streamingSubscriptionConnection.OnDisconnect        += _streamingSubscriptionConnection_OnDisconnect;
            _streamingSubscriptionConnection.OnSubscriptionError += _streamingSubscriptionConnection_OnSubscriptionError;

            try
            {
                _streamingSubscriptionConnection.Open();
            }
            catch (Exception ex)
            {
                AddLog(String.Format("Error opening subscription connection: {0}", ex.Message));
                return(false);
            }

            AddLog("Successfully subscribed for notifications");
            return(true);
        }
Exemplo n.º 3
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var service = new ExchangeService();

            if (this.DefaultCredentials.IsChecked == true)
            {
                service.UseDefaultCredentials = true;
            }
            else
            {
                service.Credentials = new WebCredentials(this.Email.Text, this.Password.Password);
            }
            service.AutodiscoverUrl(this.Email.Text);



            int counter = 0;

            using (SDK.Database database = new SDK.Database())
            {
                database.Connect();
                foreach (var checkinStyle in CheckinStyles)
                {
                    if (checkinStyle.Link)
                    {
                        Folder folder = new Folder(service);
                        folder.DisplayName = checkinStyle.Name;
                        folder.Save(WellKnownFolderName.Inbox);

                        SDK.CheckinStyle sourceStyle = new SDK.CheckinStyle(database, checkinStyle.GetUri());

                        AlternateId alternateId = new AlternateId();
                        alternateId.Format   = IdFormat.EwsId;
                        alternateId.Mailbox  = this.Email.Text;
                        alternateId.UniqueId = folder.Id.UniqueId;

                        string id = ((AlternateId)service.ConvertId(alternateId, IdFormat.HexEntryId)).UniqueId; // convert the folder ID we get from EWS to the Id Outlook expects

                        var place = sourceStyle.FindOrCreatePlace(SDK.CheckinPlaceTypes.MailForClientProcessing, id);

                        // the Checkin Place name must include the name of the Outlook profile to cater for environments that use multiple Outlook Profiles
                        place.Name = $"{place.Name}:{this.ProfileName.Text}";
                        place.Save();

                        counter++;
                    }
                }
            }

            MessageBox.Show($"Created {counter} folders.");
        }
        public String Get_EwsIdFromEntryID(ExchangeService oExchangeService, String sSmtp, String sEntryID)
        {
            // https://blogs.msdn.microsoft.com/brijs/2010/09/09/how-to-convert-exchange-items-entryid-to-ews-unique-itemid-via-ews-managed-api-convertid-call/

            // Create a request to convert identifiers.
            AlternateId objAltID = new AlternateId();

            objAltID.Format   = IdFormat.EntryId;
            objAltID.Mailbox  = sSmtp;
            objAltID.UniqueId = sEntryID;

            //Convert  PR_ENTRYID identifier format to an EWS identifier.
            AlternateIdBase objAltIDBase = oExchangeService.ConvertId(objAltID, IdFormat.EwsId);
            AlternateId     objAltIDResp = (AlternateId)objAltIDBase;

            return(objAltIDResp.UniqueId);
        }
Exemplo n.º 5
0
        public static DialogResult ShowDialog(out ItemId itemId, ExchangeService _CurrentService)
        {
            itemId = null;

            ItemIdDialog diag = new ItemIdDialog();

            diag.Text = "Enter a Item's Id...";



            DialogResult res = diag.ShowDialog();

            if (res == DialogResult.OK)
            {
                if (diag.cmboIdType.Text == "UniqueId")
                {
                    itemId = new ItemId(diag.txtUniqueId.Text);
                }

                if (diag.cmboIdType.Text == "StoreId")
                {
                    string sUniqueId = string.Empty;

                    AlternateId FromId = new AlternateId(
                        IdFormat.StoreId,
                        diag.txtUniqueId.Text.Trim(),
                        diag.txtSmtpAddress.Text.Trim()
                        );
                    //FromId.Format = IdFormat.StoreId;
                    //FromId.Mailbox = diag.txtSmtpAddress.Text.Trim();
                    //FromId.UniqueId = diag.txtUniqueId.Text.Trim();

                    AlternateId ToId =
                        (AlternateId)_CurrentService.ConvertId(FromId, IdFormat.EwsId);

                    sUniqueId = ToId.UniqueId;

                    itemId = new ItemId(diag.txtUniqueId.Text);
                }
            }

            return(res);
        }
Exemplo n.º 6
0
        private void ConvertButton_Click(object sender, EventArgs e)
        {
            if (this.srcFormatCombo.SelectedItem.HasValue == false &&
                this.reqFormatCombo.SelectedItem.HasValue == false)
            {
                if (this.srcFormatCombo.SelectedItem.HasValue == false)
                {
                    DebugLog.WriteVerbose("srcFormatCombo has no value");
                }
                if (this.reqFormatCombo.SelectedItem.HasValue == false)
                {
                    DebugLog.WriteVerbose("reqFormatCombo has no value");
                }

                ErrorDialog.ShowWarning("Select a source and requested ID format before converting.");
                return;
            }

            try
            {
                this.Cursor = Cursors.WaitCursor;

                AlternateId srcId = new AlternateId(
                    this.srcFormatCombo.SelectedItem.Value,
                    this.SrcIdText.Text,
                    this.SrcMbxText.Text);

                AlternateId cvrtId = (AlternateId)this.CurrentService.ConvertId(srcId, this.reqFormatCombo.SelectedItem.Value);

                this.CvrtIdText.Text    = System.Enum.GetName(typeof(IdFormat), cvrtId.Format);
                this.CvrtIdMbxText.Text = cvrtId.Mailbox;
                this.CvrtIdText.Text    = cvrtId.UniqueId;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
Exemplo n.º 7
0
 public override string GetPersistenceKey() => AlternateId.GetPersistenceKey();