public async Task <IActionResult> SendEmail([FromBody] ContactRequest request)
        {
            var result = false;

            try
            {
                _log.LogEntrance(nameof(SendEmail), request.ToString());


                var checkGoogle = await GoogleHelper.CallGoogleSiteVerifyAsync(request.CaptchaRequest);

                if (checkGoogle.IsSuccessStatusCode)
                {
                    var responseStr = await checkGoogle.Content.ReadAsStringAsync();

                    _log.LogInformation($"response from google : {responseStr}");

                    var googleResponse = JsonConvert.DeserializeObject <GoogleVerificationResponse>(responseStr,
                                                                                                    new JsonSerializerSettings()
                    {
                        Error = (s, e) => { e.ErrorContext.Handled = true; }
                    });

                    if (googleResponse.Success)
                    {
                        var sender = (IMailSender) new SmtpSender();
                        sender.Authenticate(_runtimeConfig.SmtpServer, _runtimeConfig.SmtpUserName, _runtimeConfig.SmtpPassword);
                        sender.SendPlain(_runtimeConfig.SmtpUserName,
                                         _runtimeConfig.ContactUsEmails,
                                         request.Subject,
                                         $"From : {request.EMail}\nRequest : {request.Body}"
                                         );

                        _log.LogInformation($"message sent successfull : {request.ToString()}");
                        result = true;
                    }
                    else
                    {
                        var errors = googleResponse.ErrorCodes == null || googleResponse.ErrorCodes.Count < 1 ?
                                     "error from google is unclear" : string.Join(",", googleResponse.ErrorCodes);

                        return(NotFound(errors));
                    }
                }
                else
                {
                    return(NotFound("Error in communication with google."));
                }

                return(Ok(result));
            }
            catch (Exception ex)
            {
                return(NotFound($"my exception : [{ex.Message}]  [{ex.StackTrace}]"));
            }
            finally
            {
                _log.LogExit(nameof(SendEmail), result.ToString());
            }
        }
Beispiel #2
0
        public void Test1()
        {
            var captchaEncrypted = @"03AOLTBLQqSv7BqJvZBSSd6ieO1FGaq7pnyn7JY_yXDoKpKwcJXDq8buuP6jBL5B5UjhVP7votjIVX2_Ok08PgrxMK0MDmlLRwScCdH_DxP7Bx6_fIpyJ-fAU2jD96-wnGMVpHNwu3tGM1UajJcSQw580NFV9yndSLCp3G4clrHmlaYaOUqgwOh2CCeKiGy8-Pw_NAYZI58Ii6QI3OqF-vwxV1fdsUrh88oXcVitc2Ad8N0fLqY68HvBmVYjzkW1Zvx77nm4Q6cEC8Twf4TYVcB_IBnLfbiz59klCfBB0b8aChW0zXn0uAzVn5XZJpVxM2BgUrtyaAaMJbh9Vvxi-gCJN76TtLFM8aTI7lGhtGulw99sh30N4lDGY064S7jJ9iOx8Cm1QoO6IS";

            var googleResponseTask = GoogleHelper.CallGoogleSiteVerifyAsync(captchaEncrypted);

            while (!googleResponseTask.IsCompleted)
            {
                Thread.Sleep(200);
            }

            if (googleResponseTask.Result.IsSuccessStatusCode)
            {
                var readAsStrTask = googleResponseTask.Result.Content.ReadAsStringAsync();

                while (!readAsStrTask.IsCompleted)
                {
                    Thread.Sleep(200);
                }


                output.WriteLine($"raw result = {readAsStrTask.Result}");


                var googleResponse =
                    JsonConvert.DeserializeObject <GoogleVerificationResponse>(readAsStrTask.Result,
                                                                               new JsonSerializerSettings()
                {
                    Error = (s, e) => { e.ErrorContext.Handled = true; },
                    //Error = HandleErrors,
                });

                output.WriteLine($"google success = {googleResponse.Success}");
                output.WriteLine($"google host = {googleResponse.hostname}");
                var errors = googleResponse.ErrorCodes == null ? "" : string.Join(',', googleResponse.ErrorCodes);
                output.WriteLine($"google errors = {errors} ");

                if (googleResponse.Success)
                {
                    output.WriteLine($"Successful recaptcha!");
                }
            }

            Assert.Equal(1, 1);
        }