示例#1
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;
                }
            }
        }
        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);
                }
            }
        }