Пример #1
0
        // Send an email message from the current user.
        public async Task <string> SendEmail(string accessToken, MessageRequest email)
        {
            string endpoint = "https://graph.microsoft.com/v1.0/me/sendMail";

            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint))
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                    request.Content = new StringContent(JsonConvert.SerializeObject(email), Encoding.UTF8, "application/json");
                    using (var response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return(Resource.Graph_SendMail_Success_Result);
                        }
                        return(response.ReasonPhrase);
                    }
                }
            }
        }
        // Send an email message from the current user.
        public async Task<string> SendEmail(string accessToken, MessageRequest email)
        {
            string endpoint = "https://graph.microsoft.com/v1.0/me/sendMail";
            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint))
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                    // This header has been added to identify our sample in the Microsoft Graph service. If extracting this code for your project please remove.
                    request.Headers.Add("SampleID", "aspnet-connect-rest-sample");               
                    request.Content = new StringContent(JsonConvert.SerializeObject(email), Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return Resource.Graph_SendMail_Success_Result;
                        }
                        return response.ReasonPhrase;
                    }
                }
            }
        }
Пример #3
0
        // Send an email message from the current user.
        public async Task <string> SendEmail(string accessToken, MessageRequest email)
        {
            string endpoint = "https://graph.microsoft.com/v1.0/me/sendMail";

            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint))
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                    // This header has been added to identify our sample in the Microsoft Graph service. If extracting this code for your project please remove.
                    request.Headers.Add("SampleID", "aspnet-connect-rest-sample");
                    request.Content = new StringContent(JsonConvert.SerializeObject(email), Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return(Resource.Graph_SendMail_Success_Result);
                        }
                        return(response.ReasonPhrase);
                    }
                }
            }
        }
        // Test GraphService.SendEmail method. 
        // Sends an email to the test account from the test account.
        // Success: Retrieved message body matches sent message body.
        public async Task SendEmail()
        {
            // Arrange
            string testBody = Guid.NewGuid().ToString();
            GraphService graphService = new GraphService();
            List<Recipient> recipientList = new List<Recipient>();
            recipientList.Add(new Recipient
            {
                EmailAddress = new UserInfo
                {
                    Address = userName
                }
            });
            Message message = new Message
            {
                Body = new ItemBody
                {
                    Content = "<html><body>" + testBody + "</body></html>",
                    ContentType = "HTML"
                },
                Subject = "Test email from ASP.NET 4.6 Connect sample",
                ToRecipients = recipientList
            };
            MessageRequest email = new MessageRequest
            {
                Message = message,
                SaveToSentItems = true
            };

            try
            {
                // Act
                string response = await graphService.SendEmail(accessToken, email);
            }

            // Test action will throw an exception when it tries to return resource string.
            // So catch the exception when the operation is complete, and then retrieve the email.
            catch (Exception e)
            {
                if (e.HResult == -2147024894)
                {
                    var mailBody = await GetLastEmail(accessToken);

                    // Assert
                    Assert.IsTrue(mailBody == testBody, mailBody);
                    return;
                }
                else
                {
                    Assert.Fail(e.InnerException.ToString());
                }
            }
            Assert.Fail();
        }