public static async Task<SendMessageResponse> PostMessageAsync(string accessToken, string userId, string message)
        {
            var url = $"https://graph.facebook.com/v2.5/{userId}/feed";
            var postbody = $"access_token={accessToken}&" +
                              "format=json&" +
                              $"message={Uri.EscapeDataString(message)}";
            var sendMessageResponse = new SendMessageResponse { Status = SendMessageStatusEnum.NotSent };

            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, url))
                {

                    request.Content = new StringContent(postbody, Encoding.UTF8, "application/x-www-form-urlencoded");
                    using (var response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            sendMessageResponse.Status = SendMessageStatusEnum.Sent;
                        }
                        else
                        {
                            JToken error = JObject.Parse(await response.Content.ReadAsStringAsync())["error"];
                            var fbError = JsonConvert.DeserializeObject<FacebookError>(error.ToString());
                            sendMessageResponse.Status = SendMessageStatusEnum.Fail;
                            sendMessageResponse.StatusMessage = fbError.error_user_msg;
                        }
                    }
                }
            }

            return sendMessageResponse;
        }
        public async Task<ActionResult> SendMessageSubmit(UserInfo userInfo)
        {
            // After Index method renders the View, user clicks Send Mail, which comes in here.
            EnsureUser(ref userInfo);
            SendMessageResponse sendMessageResult = new SendMessageResponse();
            // Send email using the Microsoft Graph API.
            var token = Data.GetUserSessionTokenAny(Settings.GetUserAuthStateId(ControllerContext.HttpContext));

            if (token.Provider == Settings.AzureADAuthority || token.Provider == Settings.AzureAD2Authority)
            {
                sendMessageResult = await GraphApiHelper.SendMessageAsync(
                    token.AccessToken,
                    GenerateEmail(userInfo));
            }
            else if (token.Provider == Settings.GoogleAuthority)
            {
                sendMessageResult = await GoogleApiHelper.SendMessageAsync(token.AccessToken, GenerateEmail(userInfo), token.Username);
            }
            // 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 },
            }));
        }
        public static async Task<SendMessageResponse> SendMessageAsync(string accessToken, SendMessageRequest sendMessageRequest)
        {
            var sendMessageResponse = new SendMessageResponse { Status = SendMessageStatusEnum.NotSent };

            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, Settings.SendMessageUrl))
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                    request.Content = new StringContent(JsonConvert.SerializeObject(sendMessageRequest), Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            sendMessageResponse.Status = SendMessageStatusEnum.Sent;
                            sendMessageResponse.StatusMessage = null;
                        }
                        else
                        {
                            sendMessageResponse.Status = SendMessageStatusEnum.Fail;
                            sendMessageResponse.StatusMessage = response.ReasonPhrase;
                        }
                    }
                }
            }

            return sendMessageResponse;
        }
        public ActionResult Facebook(SendMessageResponse sendMessageResponse, UserInfo userInfo)
        {
            EnsureUser(ref userInfo);

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

            return View();
        }
        public static async Task<SendMessageResponse> SendMessageAsync(string accessToken,
            SendMessageRequest sendMessageRequest, string username)
        {
            var message = new MimeMessage();
            //message.From.Add(new MailboxAddress(sendMessageRequest.Message.));
            foreach (var to in sendMessageRequest.Message.ToRecipients)
            {
                message.To.Add(new MailboxAddress(to.EmailAddress.Name, to.EmailAddress.Address));
            }
            message.Subject = sendMessageRequest.Message.Subject;

            var builder = new BodyBuilder();

            // Set the plain-text version of the message text
            //builder.TextBody = @"";

            // Set the html version of the message text
            builder.HtmlBody = sendMessageRequest.Message.Body.Content;

            // Now we just need to set the message body and we're done
            message.Body = builder.ToMessageBody();
            var encodedEmail = Base64UrlEncode(message.ToString());
            var url = $"https://www.googleapis.com/upload/gmail/v1/users/{username}/messages/send?uploadType=media";
            var sendMessageResponse = new SendMessageResponse { Status = SendMessageStatusEnum.NotSent };
            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, url))
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                    request.Content = new StringContent(encodedEmail, Encoding.UTF8, "message/rfc822");
                    using (var response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            sendMessageResponse.Status = SendMessageStatusEnum.Sent;
                            sendMessageResponse.StatusMessage = null;
                        }
                        else
                        {
                            sendMessageResponse.Status = SendMessageStatusEnum.Fail;
                            sendMessageResponse.StatusMessage = response.ReasonPhrase;
                        }
                    }
                }
            }
            return sendMessageResponse;
        }