// POST: api/Email public IHttpActionResult Post(EmailRequest emailRequest) { try { //Build the request model for the service to send the email PigeonRequest pigeonRequest = EmailFactory.CreatePigeonRequest(emailRequest); PigeonResponse pigeonResponse = _emailProvider.SendAFitPigeon(pigeonRequest); if (pigeonResponse.Success) { //Create the Api response for the successful post request EmailResponse emailResponse = EmailFactory.CreateEmailResponse(pigeonResponse); //Save to database if the email is sent successfully _emailSavingService = new EmailSavingService(_commonProvider, _databaseRepoProvider, emailRequest, emailResponse); //request = CommonUtility.SerializeObject(emailRequest); //response = CommonUtility.SerializeObject(emailResponse); //var isSaved = databaseStuff.SaveaSomethingAwesome(request, response, 1, 2); return(Ok(emailResponse)); } else { return(BadRequest("Response was not a success: " + pigeonResponse.Message)); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public static EmailResponse CreateEmailResponse(PigeonResponse pigeonResponse) { EmailResponse emailResponse = new EmailResponse(); emailResponse.Body = pigeonResponse.Body; emailResponse.From = pigeonResponse.From; emailResponse.Message = pigeonResponse.Message; emailResponse.SubmittedDateTime = pigeonResponse.SubmittedDateTime; emailResponse.Success = pigeonResponse.Success; emailResponse.To = pigeonResponse.To; return(emailResponse); }
public PigeonResponse SendAFitPigeon(PigeonRequest pigeonRequest) { PigeonResponse pigeonResponse = new PigeonResponse(); try { var emailSettings = Settings.SurrenderEmailSettings(); NetworkCredential networkCredential = new NetworkCredential(); networkCredential.UserName = emailSettings.Username; networkCredential.Password = emailSettings.Password; SmtpClient smtpClient = new SmtpClient(); smtpClient.Host = emailSettings.Host; smtpClient.Port = int.Parse(emailSettings.Port); smtpClient.EnableSsl = true; smtpClient.UseDefaultCredentials = true; smtpClient.Credentials = networkCredential; ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return(true); }; MailMessage mailMessage = new MailMessage(); MailAddress mailAddress = new MailAddress(emailSettings.MailAddress); mailMessage.From = mailAddress; mailMessage.To.Add(pigeonRequest.EmailAddress); mailMessage.Subject = pigeonRequest.Subject; mailMessage.Body = pigeonRequest.Message; mailMessage.IsBodyHtml = true; smtpClient.Send(mailMessage); pigeonResponse.Success = true; pigeonResponse.Message = "Email submitted to email provider successfully."; pigeonResponse.SubmittedDateTime = DateTime.Now; pigeonResponse.To = pigeonRequest.EmailAddress; pigeonResponse.From = mailAddress.Address; pigeonResponse.Body = pigeonRequest.Message; } catch (Exception ex) { pigeonResponse.Success = false; pigeonResponse.Message = "An error has occurred: " + ex.Message; pigeonResponse.SubmittedDateTime = null; } return(pigeonResponse); }