private void CloseCurrentSession()
 {
     viewerHelper = null;
     dataGridView_FolderProps.Rows.Clear();
     dataGridView_FolderProps.Columns.Clear();
     treeView_Mailbox.Nodes.Clear();
 }
        private async void FocusedInboxOverrideForm_LoadAsync(object sender, EventArgs e)
        {
            viewerHelper = new ViewerHelper.ViewerHelper(pca, currentUser);

            try
            {
                // Get all focused inbox overrides.
                originalOverrides = await viewerHelper.GetFocusedInboxOverridesAsync();

                // Show current setting.

                if (dataGridView_FocusedInboxOverrides.InvokeRequired)
                {
                    dataGridView_FocusedInboxOverrides.Invoke(new MethodInvoker(delegate
                    {
                        dataGridView_FocusedInboxOverrides.Columns.Add("Id", "Id");
                        dataGridView_FocusedInboxOverrides.Columns.Add("ClassifyAs", "ClassifyAs");
                        dataGridView_FocusedInboxOverrides.Columns.Add("DisplayName", "DisplayName");
                        dataGridView_FocusedInboxOverrides.Columns.Add("EmailAddress", "EmailAddress");
                    }));
                }
                else
                {
                    dataGridView_FocusedInboxOverrides.Columns.Add("Id", "Id");
                    dataGridView_FocusedInboxOverrides.Columns.Add("ClassifyAs", "ClassifyAs");
                    dataGridView_FocusedInboxOverrides.Columns.Add("DisplayName", "DisplayName");
                    dataGridView_FocusedInboxOverrides.Columns.Add("EmailAddress", "EmailAddress");
                }

                foreach (var item in originalOverrides)
                {
                    DataGridViewRow itemRow = new DataGridViewRow();

                    itemRow.CreateCells(dataGridView_FocusedInboxOverrides, new object[] { item.Id, item.ClassifyAs.ToString(), item.SenderEmailAddress.Name, item.SenderEmailAddress.Address });

                    if (dataGridView_FocusedInboxOverrides.InvokeRequired)
                    {
                        dataGridView_FocusedInboxOverrides.Invoke(new MethodInvoker(delegate { dataGridView_FocusedInboxOverrides.Rows.Add(itemRow); }));
                    }
                    else
                    {
                        dataGridView_FocusedInboxOverrides.Rows.Add(itemRow);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
        }
        private bool Prepare()
        {
            // Use MSAL and acquire access token.

            AcquireViewerTokenForm acuireViewerTokenForm = new AcquireViewerTokenForm();

            if (acuireViewerTokenForm.ShowDialog(out pca, out ar) != DialogResult.OK)
            {
                return(false);
            }

            string token = ar.AccessToken;

            currentUser = ar.User;

            try
            {
                viewerHelper = new ViewerHelper.ViewerHelper(pca, currentUser);

                // Get the Drafts folder.
                GetDraftsFolderAsync();

                // Get the root folder.
                PrepareMsgFolderRootAsync();

                // Create Calendar dummy root folder.
                PrepareCalendarRootFolder();

                // Create TaskGroup dummy root folder.
                PrepareTaskGroupRootFolder();

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("ERROR retrieving folders: {0}", ex.Message, "Office365APIEditor"));

                return(false);
            }
        }
        private async void Button_OK_ClickAsync(object sender, EventArgs e)
        {
            UseWaitCursor = true;
            Enabled       = false;

            viewerHelper = new ViewerHelper.ViewerHelper(pca, currentUser);

            if (overridesToBeRemoved.Count != 0)
            {
                // A part of original override was removed.

                foreach (var item in overridesToBeRemoved)
                {
                    try
                    {
                        await viewerHelper.RemoveFocusedInboxOverrideAsync(item);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            foreach (DataGridViewRow row in dataGridView_FocusedInboxOverrides.Rows)
            {
                if (row.Cells[0].Value == null || row.Cells[0].Value.ToString() == "")
                {
                    // This is a new entry
                    // Need to add new settings.

                    FocusedInboxOverride newOverride = new FocusedInboxOverride
                    {
                        ClassifyAs         = (Classify)Enum.Parse(typeof(Classify), row.Cells[1].Value.ToString()),
                        SenderEmailAddress = new FocusedInboxOverrideSender(row.Cells[2].Value.ToString(), row.Cells[3].Value.ToString())
                    };

                    try
                    {
                        await viewerHelper.AddOrUpdateFocusedInboxOverrideAsync(newOverride);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else if (overridesToBeChanged.Contains(row.Cells[0].Value.ToString()))
                {
                    // This override is changed.

                    FocusedInboxOverride newOverride = new FocusedInboxOverride
                    {
                        ClassifyAs         = (Classify)Enum.Parse(typeof(Classify), row.Cells[1].Value.ToString()),
                        SenderEmailAddress = new FocusedInboxOverrideSender(row.Cells[2].Value.ToString(), row.Cells[3].Value.ToString())
                    };

                    try
                    {
                        await viewerHelper.AddOrUpdateFocusedInboxOverrideAsync(newOverride);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            UseWaitCursor = false;
            Close();
        }
Exemplo n.º 5
0
        private async void SendMailForm_LoadAsync(object sender, EventArgs e)
        {
            attachments = new List <FileAttachment>();

            comboBox_Importance.SelectedIndex = 1;
            comboBox_BodyType.SelectedIndex   = 0;

            if (draftItemId != "")
            {
                // Editing a draft item.

                button_Attachments.Enabled = false;

                // When sending a draft item, it must be saved to SentItems.
                checkBox_SaveToSentItems.Checked = true;
                checkBox_SaveToSentItems.Enabled = false;

                viewerHelper = new ViewerHelper.ViewerHelper(pca, currentUser);
                var draftItem = await viewerHelper.GetDraftMessageAsync(draftItemId);

                if (comboBox_Importance.InvokeRequired)
                {
                    comboBox_Importance.Invoke(new MethodInvoker(delegate
                    {
                        comboBox_Importance.SelectedIndex = (int)draftItem.Importance;
                    }));
                }
                else
                {
                    comboBox_Importance.SelectedIndex = (int)draftItem.Importance;
                }

                if (comboBox_Importance.InvokeRequired)
                {
                    comboBox_Importance.Invoke(new MethodInvoker(delegate
                    {
                        comboBox_Importance.SelectedIndex = (int)draftItem.Importance;;
                    }));
                }
                else
                {
                    comboBox_Importance.SelectedIndex = (int)draftItem.Importance;
                }

                if (checkBox_RequestDeliveryReceipt.InvokeRequired)
                {
                    checkBox_RequestDeliveryReceipt.Invoke(new MethodInvoker(delegate
                    {
                        checkBox_RequestDeliveryReceipt.Checked = draftItem.RequestDeliveryReceipt;;
                    }));
                }
                else
                {
                    checkBox_RequestDeliveryReceipt.Checked = draftItem.RequestDeliveryReceipt;
                }

                if (checkBox_RequestReadReceipt.InvokeRequired)
                {
                    checkBox_RequestReadReceipt.Invoke(new MethodInvoker(delegate
                    {
                        checkBox_RequestReadReceipt.Checked = draftItem.RequestReadReceipt;
                    }));
                }
                else
                {
                    checkBox_RequestReadReceipt.Checked = draftItem.RequestReadReceipt;
                }

                if (textBox_To.InvokeRequired)
                {
                    textBox_To.Invoke(new MethodInvoker(delegate
                    {
                        textBox_To.Text = RecipientsString(draftItem.ToRecipients);
                    }));
                }
                else
                {
                    textBox_To.Text = RecipientsString(draftItem.ToRecipients);
                }

                if (textBox_Cc.InvokeRequired)
                {
                    textBox_Cc.Invoke(new MethodInvoker(delegate
                    {
                        textBox_Cc.Text = RecipientsString(draftItem.CcRecipients);
                    }));
                }
                else
                {
                    textBox_Cc.Text = RecipientsString(draftItem.CcRecipients);
                }

                if (textBox_Bcc.InvokeRequired)
                {
                    textBox_Bcc.Invoke(new MethodInvoker(delegate
                    {
                        textBox_Bcc.Text = RecipientsString(draftItem.BccRecipients);
                    }));
                }
                else
                {
                    textBox_Bcc.Text = RecipientsString(draftItem.BccRecipients);
                }

                if (textBox_Subject.InvokeRequired)
                {
                    textBox_Subject.Invoke(new MethodInvoker(delegate
                    {
                        textBox_Subject.Text = draftItem.Subject;
                    }));
                }
                else
                {
                    textBox_Subject.Text = draftItem.Subject;
                }

                if (textBox_Body.InvokeRequired)
                {
                    textBox_Body.Invoke(new MethodInvoker(delegate
                    {
                        textBox_Body.Text = draftItem.Body;
                    }));
                }
                else
                {
                    textBox_Body.Text = draftItem.Body;
                }

                if (comboBox_BodyType.InvokeRequired)
                {
                    comboBox_BodyType.Invoke(new MethodInvoker(delegate
                    {
                        comboBox_BodyType.SelectedIndex = (int)draftItem.BodyType;
                    }));
                }
                else
                {
                    comboBox_BodyType.SelectedIndex = (int)draftItem.BodyType;
                }

                var attachList = await viewerHelper.GetAttachmentsAsync(FolderContentType.Message, draftItemId);

                foreach (var attach in attachList)
                {
                    Dictionary <string, string> fullAttachInfo = await viewerHelper.GetAttachmentAsync(FolderContentType.Message, draftItemId, attach.Id);

                    string tempType         = "";
                    string tempName         = "";
                    string tempContentBytes = "";

                    foreach (KeyValuePair <string, string> item in fullAttachInfo)
                    {
                        if (item.Key == "@odata.type")
                        {
                            tempType = (item.Value == null) ? "" : item.Value.ToString();
                        }
                        else if (item.Key == "Name")
                        {
                            tempName = (item.Value == null) ? "" : item.Value.ToString();
                        }
                        else if (item.Key == "ContentBytes")
                        {
                            tempContentBytes = (item.Value == null) ? "" : item.Value.ToString();
                        }
                    }

                    if (tempType == "#Microsoft.OutlookServices.FileAttachment")
                    {
                        // This is a FileAttachment

                        attachments.Add(new FileAttachment(tempName, tempContentBytes));
                    }
                }

                if (button_Attachments.InvokeRequired)
                {
                    button_Attachments.Invoke(new MethodInvoker(delegate
                    {
                        button_Attachments.Enabled = true;
                    }));
                }
                else
                {
                    button_Attachments.Enabled = true;
                }
            }
        }
Exemplo n.º 6
0
        private async void Button_Save_Click(object sender, EventArgs e)
        {
            // Save new mail.

            Enabled = false;

            viewerHelper = new ViewerHelper.ViewerHelper(pca, currentUser);

            NewEmailMessage newItem;

            try
            {
                newItem = CreateNewEmailMessageObject();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Enabled = true;
                return;
            }

            try
            {
                if (draftItemId == "")
                {
                    await viewerHelper.SaveDraftAsync(newItem);
                }
                else
                {
                    // This is a draft message.
                    await viewerHelper.UpdateDraftAsync(draftItemId, newItem);
                }

                Close();
            }
            catch (WebException ex)
            {
                if (ex.Response == null)
                {
                    MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    string jsonResponse = "";
                    using (Stream responseStream = ex.Response.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(responseStream, Encoding.Default);
                        jsonResponse = reader.ReadToEnd();
                    }

                    HttpWebResponse response = (HttpWebResponse)ex.Response;

                    MessageBox.Show(response.StatusCode.ToString() + Environment.NewLine + jsonResponse + Environment.NewLine, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Enabled = true;
            }
        }
        private async void AttachmentViewerForm_Load(object sender, EventArgs e)
        {
            Text = "Attachments for '" + targetItemSubject + "'";

            viewerHelper = new ViewerHelper.ViewerHelper(pca, currentUser);

            List <AttachmentSummary> result = new List <AttachmentSummary>();;

            switch (targetFolder.Type)
            {
            case FolderContentType.Message:
            case FolderContentType.MsgFolderRoot:
            case FolderContentType.Drafts:
                try
                {
                    result = await viewerHelper.GetAttachmentsAsync(FolderContentType.Message, targetItemId);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                break;

            case FolderContentType.Contact:
                // In the implementation of OutlookServicesClient, contact item does not have attachment.

                break;

            case FolderContentType.Calendar:
                try
                {
                    result = await viewerHelper.GetAttachmentsAsync(FolderContentType.Calendar, targetItemId);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                break;

            case FolderContentType.DummyCalendarRoot:
                break;

            default:
                break;
            }

            foreach (var attachment in result)
            {
                // Add new row.

                string name        = attachment.Name;
                string id          = attachment.Id;
                string contentType = attachment.ContentType;

                DataGridViewRow itemRow = new DataGridViewRow
                {
                    Tag = attachment.Id
                };
                itemRow.CreateCells(dataGridView_AttachmentList, new object[] { name, id, contentType });
                // itemRow.ContextMenuStrip = contextMenuStrip_AttachmentList;

                if (dataGridView_AttachmentList.InvokeRequired)
                {
                    dataGridView_AttachmentList.Invoke(new MethodInvoker(delegate { dataGridView_AttachmentList.Rows.Add(itemRow); }));
                }
                else
                {
                    dataGridView_AttachmentList.Rows.Add(itemRow);
                }
            }
        }
        private async void FolderViewerForm_Load(object sender, System.EventArgs e)
        {
            toolStripStatusLabel_Status.Text = "Loading all items...";

            string typeForWindowTitle = "";

            switch (targetFolder.Type)
            {
            case FolderContentType.Message:
            case FolderContentType.MsgFolderRoot:
            case FolderContentType.Drafts:
                typeForWindowTitle = "Message items";
                break;

            case FolderContentType.Contact:
                typeForWindowTitle = "Contact items";
                break;

            case FolderContentType.Calendar:
                typeForWindowTitle = "Calendar items";
                break;

            case FolderContentType.Task:
                typeForWindowTitle = "Task items";
                break;

            default:
                typeForWindowTitle = "items";
                break;
            }

            Text = typeForWindowTitle + " in " + targetFolderDisplayName;

            viewerHelper = new ViewerHelper.ViewerHelper(pca, currentUser);

            switch (targetFolder.Type)
            {
            case FolderContentType.Message:
                // Add columns.
                PrepareMessageItemListColumns();

                // Get items.
                List <MessageSummary> messages = await viewerHelper.GetMessageSummaryAsync(targetFolder.ID);

                if (messages.Count != 0)
                {
                    ShowMessages(messages);
                }
                else
                {
                    var mailFolder = await viewerHelper.GetMailFolderAsync(targetFolder.ID, targetFolderDisplayName);

                    if (mailFolder.TotalItemCount != 0 && MessageBox.Show("TotalItemCount of this folder is not 0 but getting items of this folder was failed.\r\nDo you want to retry getting items as Contact item?", "Office365APIEditor", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        // Retry as Contact item.

                        targetFolder.Type = FolderContentType.Contact;

                        Text = FolderContentType.Contact.ToString() + " items in " + targetFolderDisplayName;

                        // Reset DataGrid.
                        if (dataGridView_ItemList.InvokeRequired)
                        {
                            dataGridView_ItemList.Invoke(new MethodInvoker(delegate
                            {
                                for (int i = dataGridView_ItemList.Columns.Count - 1; i >= 0; i--)
                                {
                                    dataGridView_ItemList.Columns.RemoveAt(i);
                                }
                            }));
                        }
                        else
                        {
                            for (int i = dataGridView_ItemList.Columns.Count - 1; i >= 0; i--)
                            {
                                dataGridView_ItemList.Columns.RemoveAt(i);
                            }
                        }

                        // Add columns.
                        PrepareContactItemListColumns();

                        // Get items.
                        List <ContactSummary> contactsInMailFolder = await viewerHelper.GetContactSummaryAsync(targetFolder.ID);

                        ShowContacts(contactsInMailFolder);
                    }
                }

                break;

            case FolderContentType.Contact:
                // Add columns.
                PrepareContactItemListColumns();

                // Get items.
                List <ContactSummary> contacts = await viewerHelper.GetContactSummaryAsync(targetFolder.ID);

                ShowContacts(contacts);

                break;

            case FolderContentType.Calendar:
                // Add columns.
                PrepareCalendarItemListColumns();

                // Get items.
                List <EventSummary> events = await viewerHelper.GetEventSummaryAsync(targetFolder.ID);

                ShowEvents(events);

                break;

            case FolderContentType.MsgFolderRoot:
            case FolderContentType.Drafts:
                // Add columns.
                PrepareMessageItemListColumns();

                // Get items.
                List <MessageSummary> messagesInMsgFolderRoot = await viewerHelper.GetMessageSummaryAsync(targetFolder.ID);

                if (messagesInMsgFolderRoot.Count != 0)
                {
                    ShowMessages(messagesInMsgFolderRoot);
                }

                break;

            case FolderContentType.Task:
                // Add Columns.
                PrepareTaskItemListColumns();

                // Get items.
                List <TaskSummary> tasks = await viewerHelper.GetTaskSummaryAsync(targetFolder.ID);

                if (tasks.Count != 0)
                {
                    ShowTasks(tasks);
                }

                break;

            case FolderContentType.DummyCalendarRoot:
            case FolderContentType.DummyTaskGroupRoot:
            case FolderContentType.TaskGroup:
            default:
                break;
            }

            toolStripStatusLabel_Status.Text = "Loaded all items.";
        }