Esempio n. 1
0
        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());
            }
        }