示例#1
0
        private string CreatePostDataToSave(NewEmailMessage newItem)
        {
            // Create POST data for sending or saving new item from NewEmailMessage.

            string postData = @"{
        ""ReplyTo"": [],
        ""ToRecipients"": [{ToRecipients}],
        ""CcRecipients"": [{CcRecipients}],
        ""BccRecipients"": [{BccRecipients}],
        ""Subject"": ""{Subject}"",
        ""Body"": {
            ""ContentType"": ""{ContentType}"",
            ""Content"": ""{Content}""
        },
        ""Importance"": ""{Importance}"",
        ""IsDeliveryReceiptRequested"": {IsDeliveryReceiptRequested},
        ""IsReadReceiptRequested"": {IsReadReceiptRequested},
        ""Categories"": [],
        ""Attachments"": [{Attachments}]
    }";

            postData = CreateMessageBase(newItem, postData);

            return(postData);
        }
示例#2
0
        private string CreatePostDataToSendNewMessageOnTheFly(NewEmailMessage newItem)
        {
            // Create POST data for sending a new message on the fly.

            string postData = @"{
    ""Message"": {
        ""ReplyTo"": [],
        ""ToRecipients"": [{ToRecipients}],
        ""CcRecipients"": [{CcRecipients}],
        ""BccRecipients"": [{BccRecipients}],
        ""Subject"": ""{Subject}"",
        ""Body"": {
            ""ContentType"": ""{ContentType}"",
            ""Content"": ""{Content}""
        },
        ""Importance"": ""{Importance}"",
        ""IsDeliveryReceiptRequested"": {IsDeliveryReceiptRequested},
        ""IsReadReceiptRequested"": {IsReadReceiptRequested},
        ""Categories"": [],
        ""Attachments"": [{Attachments}]
    },
    ""SaveToSentItems"": ""{SaveToSentItems}""
}";

            postData = CreateMessageBase(newItem, postData);

            postData = postData.Replace("{SaveToSentItems}", newItem.SaveToSentItems.ToString().ToLower());

            return(postData);
        }
示例#3
0
        public async Task <NewEmailMessage> GetDraftMessageAsync(string draftItemId)
        {
            // Get the specified item as a draft item to send.

            try
            {
                var draftItem = await GetMessageAsync(draftItemId);

                NewEmailMessage newEmailMessage = new NewEmailMessage
                {
                    ToRecipients           = ConvertRecipientIListToMailAddressCollection(draftItem.ToRecipients),
                    CcRecipients           = ConvertRecipientIListToMailAddressCollection(draftItem.CcRecipients),
                    BccRecipients          = ConvertRecipientIListToMailAddressCollection(draftItem.BccRecipients),
                    Subject                = draftItem.Subject ?? "",
                    BodyType               = (draftItem.Body != null) ? (BodyType)Enum.Parse(typeof(BodyType), draftItem.Body.ContentType, true) : BodyType.Text,
                    Body                   = (draftItem.Body != null && draftItem.Body.Content != null) ? draftItem.Body.Content : "",
                    Importance             = (draftItem.Importance != null) ? (Importance)Enum.Parse(typeof(Importance), draftItem.Importance, true) : Importance.Normal,
                    RequestDeliveryReceipt = (draftItem.IsDeliveryReceiptRequested != null && draftItem.IsDeliveryReceiptRequested.HasValue) ? draftItem.IsDeliveryReceiptRequested.Value : false,
                    RequestReadReceipt     = (draftItem.IsReadReceiptRequested != null && draftItem.IsReadReceiptRequested.HasValue) ? draftItem.IsReadReceiptRequested.Value : false
                };

                return(newEmailMessage);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#4
0
        public async Task UpdateDraftAsync(string draftItemId, NewEmailMessage newItem)
        {
            // Update a draft mail.

            try
            {
                // First, update the draft item.

                Uri URL;
                URL = new Uri($"https://outlook.office.com/api/v2.0/Me/messages/{draftItemId}");

                string postData = CreatePostDataToUpdateDraft(newItem);

                string result = await SendPatchRequestAsync(URL, postData);

                // Then, remove all attachment once.

                var currentAttachments = await GetAllAttachmentsAsync(FolderContentType.Message, draftItemId);

                foreach (var attach in currentAttachments)
                {
                    URL    = new Uri($"https://outlook.office.com/api/v2.0/Me/messages/{draftItemId}/attachments/{attach.Id}");
                    result = await SendDeleteRequestAsync(URL);
                }

                // Finally, upload new attachments.

                string attachmentTemplate = @"{
            ""@odata.type"": ""#Microsoft.OutlookServices.FileAttachment"",
            ""Name"": ""{FileName}"",
            ""ContentBytes"": ""{ContentBytes}""
        }";

                foreach (var attach in newItem.Attachments)
                {
                    if (attach is Data.AttachmentAPI.FileAttachment)
                    {
                        postData = attachmentTemplate.Replace("{FileName}", attach.Name);
                        postData = postData.Replace("{ContentBytes}", ((Data.AttachmentAPI.FileAttachment)attach).ContentBytes);
                    }

                    URL    = new Uri($"https://outlook.office.com/api/v2.0/Me/messages/{draftItemId}/attachments/");
                    result = await SendPostRequestAsync(URL, postData);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#5
0
        public async Task UpdateDraftAsync(string draftItemId, NewEmailMessage newItem)
        {
            // Update a draft mail.

            try
            {
                // First, update the draft item.

                Uri URL;
                URL = Util.UseMicrosoftGraphInMailboxViewer ? new Uri($"https://graph.microsoft.com/v1.0/Me/messages/{draftItemId}") : new Uri($"https://outlook.office.com/api/v2.0/Me/messages/{draftItemId}");

                string postData = CreatePostDataToUpdateDraft(newItem);

                // Remove attachment information to reduce the size.
                string postDataWithoutAttachment = Regex.Replace(postData, @"\""attachments\"":\[.*\],", @"""attachments"":[],", RegexOptions.Singleline);

                string result = await SendPatchRequestAsync(URL, postDataWithoutAttachment);

                // Then, remove all attachment once.

                var currentAttachments = await GetAllAttachmentsAsync(FolderContentType.Message, draftItemId);

                foreach (var attach in currentAttachments)
                {
                    URL    = Util.UseMicrosoftGraphInMailboxViewer ? new Uri($"https://graph.microsoft.com/v1.0/Me/messages/{draftItemId}/attachments/{attach.Id}") : new Uri($"https://outlook.office.com/api/v2.0/Me/messages/{draftItemId}/attachments/{attach.Id}");
                    result = await SendDeleteRequestAsync(URL);
                }

                foreach (var attach in newItem.Attachments)
                {
                    if (attach is Data.AttachmentAPI.FileAttachment)
                    {
                        using (var stream = new MemoryStream())
                        {
                            var serializer = new DataContractJsonSerializer(typeof(Data.AttachmentAPI.FileAttachment));
                            serializer.WriteObject(stream, attach);
                            postData = Encoding.UTF8.GetString(stream.ToArray());
                        }
                    }

                    URL    = Util.UseMicrosoftGraphInMailboxViewer ? new Uri($"https://graph.microsoft.com/v1.0/Me/messages/{draftItemId}/attachments/") : new Uri($"https://outlook.office.com/api/v2.0/Me/messages/{draftItemId}/attachments/");
                    result = await SendPostRequestAsync(URL, postData);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#6
0
        private string CreatePostDataToSave(NewEmailMessage newItem)
        {
            // Create POST data for sending or saving new item from NewEmailMessage.

            string result;

            using (var stream = new MemoryStream())
            {
                var serializer = new DataContractJsonSerializer(typeof(NewEmailMessage));
                serializer.WriteObject(stream, newItem);
                result = Encoding.UTF8.GetString(stream.ToArray());
            }

            return(result);
        }
示例#7
0
        private string CreatePostDataToSendNewMessageOnTheFly(NewEmailMessage newItem, bool saveToSentItems)
        {
            string postData = @"{
    ""Message"": {messageJson},
    ""SaveToSentItems"": ""{SaveToSentItems}""
}";

            string messageJson = CreatePostDataToSave(newItem);

            postData = postData.Replace("{messageJson}", messageJson);

            postData = postData.Replace("{SaveToSentItems}", saveToSentItems.ToString().ToLower());

            return(postData);
        }
示例#8
0
        public async Task SaveDraftAsync(NewEmailMessage newItem)
        {
            // Save a new draft mail.

            Uri URL = new Uri("https://outlook.office.com/api/v2.0/Me/messages");

            string postData = CreatePostDataToSave(newItem);

            try
            {
                string stringResponse = await SendPostRequestAsync(URL, postData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#9
0
        public async Task SaveDraftAsync(NewEmailMessage newItem)
        {
            // Save a new draft mail.

            Uri URL = Util.UseMicrosoftGraphInMailboxViewer ? new Uri("https://graph.microsoft.com/v1.0/me/messages") : new Uri("https://outlook.office.com/api/v2.0/Me/messages");

            string postData = CreatePostDataToSave(newItem);

            try
            {
                string stringResponse = await SendPostRequestAsync(URL, postData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#10
0
        public async Task SendMailAsync(NewEmailMessage newItem, bool saveToSentItems)
        {
            // Send a mail.
            // SendMailAsync method of NuGet version OutlookServicesClient is not working, so we don't use that.

            Uri URL = new Uri("https://outlook.office.com/api/v2.0/Me/SendMail");

            string postData = CreatePostDataToSendNewMessageOnTheFly(newItem);

            try
            {
                string stringResponse = await SendPostRequestAsync(URL, postData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#11
0
        private string CreateMessageBase(NewEmailMessage newItem, string basePostData)
        {
            string emailAddressTemplate = @"{
            ""EmailAddress"": {
                ""Address"": ""{Address}"",
                ""Name"": ""{DisplayName}""
            }
        }";

            string attachmentTemplate = @"{
            ""@odata.type"": ""#Microsoft.OutlookServices.FileAttachment"",
            ""Name"": ""{FileName}"",
            ""ContentBytes"": ""{ContentBytes}""
        }";

            if (newItem.ToRecipients != null && newItem.ToRecipients.Count != 0)
            {
                List <string> recipientList = new List <string>();
                foreach (var recipient in newItem.ToRecipients)
                {
                    string newResipient = emailAddressTemplate.Replace("{Address}", recipient.Address).Replace("{DisplayName}", Util.EscapeForJson(recipient.DisplayName));
                    recipientList.Add(newResipient);
                }

                string recipients = string.Join(", ", recipientList);

                basePostData = basePostData.Replace("{ToRecipients}", recipients);
            }
            else
            {
                basePostData = basePostData.Replace("{ToRecipients}", "");
            }

            if (newItem.CcRecipients != null && newItem.CcRecipients.Count != 0)
            {
                List <string> recipientList = new List <string>();
                foreach (var recipient in newItem.CcRecipients)
                {
                    string newResipient = emailAddressTemplate.Replace("{Address}", recipient.Address).Replace("{DisplayName}", Util.EscapeForJson(recipient.DisplayName));
                    recipientList.Add(newResipient);
                }

                string recipients = string.Join(", ", recipientList);

                basePostData = basePostData.Replace("{CcRecipients}", recipients);
            }
            else
            {
                basePostData = basePostData.Replace("{CcRecipients}", "");
            }

            if (newItem.BccRecipients != null && newItem.BccRecipients.Count != 0)
            {
                List <string> recipientList = new List <string>();
                foreach (var recipient in newItem.BccRecipients)
                {
                    string newResipient = emailAddressTemplate.Replace("{Address}", recipient.Address).Replace("{DisplayName}", Util.EscapeForJson(recipient.DisplayName));
                    recipientList.Add(newResipient);
                }

                string recipients = string.Join(", ", recipientList);

                basePostData = basePostData.Replace("{BccRecipients}", recipients);
            }
            else
            {
                basePostData = basePostData.Replace("{BccRecipients}", "");
            }

            basePostData = basePostData.Replace("{Subject}", Util.EscapeForJson(newItem.Subject));

            basePostData = basePostData.Replace("{ContentType}", newItem.BodyType.ToString());

            basePostData = basePostData.Replace("{Content}", Util.EscapeForJson(newItem.Body));

            basePostData = basePostData.Replace("{Importance}", newItem.Importance.ToString());

            basePostData = basePostData.Replace("{IsDeliveryReceiptRequested}", newItem.RequestDeliveryReceipt.ToString().ToLower());

            basePostData = basePostData.Replace("{IsReadReceiptRequested}", newItem.RequestReadReceipt.ToString().ToLower());

            if (newItem.Attachments != null && newItem.Attachments.Count != 0)
            {
                List <string> attachmentList = new List <string>();
                foreach (var attachment in newItem.Attachments)
                {
                    if (attachment is Data.AttachmentAPI.FileAttachment)
                    {
                        string newAttachment = attachmentTemplate.Replace("{FileName}", attachment.Name).Replace("{ContentBytes}", ((Data.AttachmentAPI.FileAttachment)attachment).ContentBytes);
                        attachmentList.Add(newAttachment);
                    }
                }

                string attachments = string.Join(", ", attachmentList);

                basePostData = basePostData.Replace("{Attachments}", attachments);
            }
            else
            {
                basePostData = basePostData.Replace("{Attachments}", "");
            }

            return(basePostData);
        }
示例#12
0
        private string CreatePostDataToUpdateDraft(NewEmailMessage newItem)
        {
            // Create POST data for sending or saving new item from NewEmailMessage.

            return(CreatePostDataToSave(newItem));
        }