public ExchangeEwsPersistentConnection(ExchangeService service, string emailAddress, AutodiscoverRedirectionUrlValidationCallback autoDiscoverCallback)
 {
     this.service              = service;
     this.emailAddress         = emailAddress;
     this.autoDiscoverCallback = autoDiscoverCallback;
 }
        public async Task SendEmailWithVerificationCode(Object json)
        {
            string name;
            string username;

            SprHuman[] sprHuman;
            dynamic    jsonDynamic = JObject.Parse(json.ToString());
            //if (!CheckOutGoogleRecaptchaToken(jsonDynamic.captcha.ToString()))
            //{
            //    await Response.WriteAsync("Is invalid recaptcha token !");
            //    return;
            //}
            AutodiscoverRedirectionUrlValidationCallback autodiscoverRedirectionUrlValidationCallback = (string redirectionUrl) => { Uri redirectionUri = new Uri(redirectionUrl); if (redirectionUri.Scheme == "https")
                                                                                                                                     {
                                                                                                                                         return(true);
                                                                                                                                     }
                                                                                                                                     else
                                                                                                                                     {
                                                                                                                                         return(false);
                                                                                                                                     } };

            Microsoft.Exchange.WebServices.Data.ExchangeService exchangeService = new Microsoft.Exchange.WebServices.Data.ExchangeService(Microsoft.Exchange.WebServices.Data.ExchangeVersion.Exchange2013_SP1);
            exchangeService.Credentials = new Microsoft.Exchange.WebServices.Data.WebCredentials(AuthorizationConfig.login, AuthorizationConfig.password);
            try
            {
                string   email    = jsonDynamic.email;
                DateTime birthday = DateTime.Parse(jsonDynamic.birthdate.ToString());
                sprHuman = GetInformationFromDatabase(email);
                if (birthday != sprHuman[0].Birthday)
                {
                    await Response.WriteAsync($"Email is doesn't send !");

                    return;
                }
                name     = $"{sprHuman[0].FirstName} {sprHuman[0].LastName}";
                username = sprHuman[0].SamaccountName;
                Random random           = new Random();
                string verificationCode = AuthorizationConfig.verificationCode;
                for (int i = 0; i <= 10; i++)
                {
                    char   characterLetter = (char)random.Next('A', 'Z');
                    char   characterDigit  = (char)random.Next('1', '9');
                    char[] characters      = { characterLetter, '~', '!', characterDigit, '@', '#', characterLetter, '$', '&', characterDigit, '*', '(', characterLetter, ')', '_', characterDigit, '+', '?', characterLetter, ':', '-', characterDigit, '=', ',', characterLetter, '.', ';', characterDigit, '[', ']', characterLetter };
                    verificationCode += $"{characters[random.Next(characters.Length)]}";
                }
                verificationCodes.Add(verificationCode, username);
                exchangeService.Timeout = 300000;
                exchangeService.AutodiscoverUrl(AuthorizationConfig.login, autodiscoverRedirectionUrlValidationCallback);
                ThreadStart threadStart = () => { Action action = () => { Thread.Sleep(300000); verificationCodes.Remove(verificationCodes[verificationCode]); }; };
                Thread      thread      = new Thread(threadStart);
                thread.Start();
                Microsoft.Exchange.WebServices.Data.EmailMessage emailMessage = new Microsoft.Exchange.WebServices.Data.EmailMessage(exchangeService);
                emailMessage.ToRecipients.Add(email);
                emailMessage.Subject = "Hello World";
                emailMessage.Body    = new Microsoft.Exchange.WebServices.Data.MessageBody($"Hello {name} ! This is the verification code {verificationCode} which you must to introduce in following link http://elk-test.iep.ru/password-reset/{GenerateToken(username, AuthorizationConfig.lifeTimeForCheckOutEmailOrChangePasswordForUnauthUser)}");
                await emailMessage.SendAndSaveCopy();

                await Response.WriteAsync("Email is sent !");
            }
            catch (Exception ex)
            {
                await Response.WriteAsync($"Email is doesn't send ! {ex.Message}");
            }
        }