// Get infomation about the current logged in user.
        public static async Task<UserInfo> GetUserInfoAsync(string accessToken)
        {
            UserInfo myInfo = new UserInfo();

            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Get, Settings.GetMeUrl))
                {
                    request.Headers.Accept.Add(Json);
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                    using (var response = await client.SendAsync(request))
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            var json = JObject.Parse(await response.Content.ReadAsStringAsync());
                            myInfo.Name = json?["displayName"]?.ToString();
                            myInfo.Address = json?["mail"]?.ToString().Trim().Replace(" ", string.Empty);
                            
                        }
                    }
                }
            }

            return myInfo;
        }
 // Create email object in the required request format/data contract.
 private SendMessageRequest CreateEmailObject(UserInfo to, string subject, string body)
 {
     return new SendMessageRequest
     {
         Message = new Message
         {
             Subject = subject,
             Body = new MessageBody
             {
                 ContentType = "Html",
                 Content = body
             },
             ToRecipients = new List<Recipient>
             {
                 new Recipient
                 {
                     EmailAddress = new UserInfo
                     {
                          Name =  to.Name,
                          Address = to.Address
                     }
                 }
             }
         },
         SaveToSentItems = true
     };
 }
 // Create email with predefine body and subject.
 SendMessageRequest GenerateEmail(UserInfo to)
 {
     return CreateEmailObject(
         to: to,
         subject: Settings.MessageSubject,
         body: string.Format(Settings.MessageBody, to.Name)
     );
 }
        // Take data and put into Index view.
        public ActionResult Index(SendMessageResponse sendMessageResponse, UserInfo userInfo)
        {
            EnsureUser(ref userInfo);

            ViewBag.UserInfo = userInfo;
            ViewBag.MessageResponse = sendMessageResponse;

            return View();
        }
        // Use the login user name or recipient email address if no user name.
        void EnsureUser(ref UserInfo userInfo)
        {
            var currentUser = (UserInfo)Session[SessionKeys.Login.UserInfo];

            if (userInfo == null || string.IsNullOrEmpty(userInfo.Address))
            {
                userInfo = currentUser;
            }
            else if (userInfo.Address.Equals(currentUser.Address, StringComparison.OrdinalIgnoreCase))
            {
                userInfo = currentUser;
            }
            else
            {
                userInfo.Name = userInfo.Address;
            }
        }
        public async Task<ActionResult> SendMessageSubmit(UserInfo userInfo)
        {
            // After Index method renders the View, user clicks Send Mail, which comes in here.
            EnsureUser(ref userInfo);

            // Send email using the Microsoft Graph API.
            var sendMessageResult = await UnifiedApiHelper.SendMessageAsync(
                (string)Session[SessionKeys.Login.AccessToken],  
                GenerateEmail(userInfo));

            // Reuse the Index view for messages (sent, not sent, fail) .
            // Redirect to tell the browser to call the app back via the Index method.
            return RedirectToAction(nameof(Index), new RouteValueDictionary(new Dictionary<string,object>{
                { "Status", sendMessageResult.Status },
                { "StatusMessage", sendMessageResult.StatusMessage },
                { "Address", userInfo.Address },
            }));
        }