public async Task <ActionResult> Subscribe(NewsLetterSubscribeModel newsLetterSubscribeModel) { var resultModel = new ResultModel(); try { if (!ModelState.IsValid) { resultModel.Result = false; resultModel.Message = "Please submit a valid email address"; return(Json(resultModel)); } var resultTuple = await newsLetterService.Subscribe(newsLetterSubscribeModel.Email); resultModel.Result = resultTuple.result; resultModel.Message = resultTuple.message; } catch (Exception e) { resultModel.Result = false; resultModel.Message = "An error occurred please contact [email protected]"; Log.Error(e); } return(Json(resultModel)); }
public async Task <ActionResult> Subscribe(NewsLetterSubscribeModel newsLetterSubscribeModel) { var resultModel = new ResultModel(); try { if (!ModelState.IsValid) { resultModel.Result = false; resultModel.Message = "Please submit a valid email address"; return(Json(resultModel)); } var resultTuple = await newsLetterService.Subscribe(newsLetterSubscribeModel.Email); resultModel.Result = resultTuple.result; resultModel.Message = resultTuple.message; MailMessage message = new MailMessage(SiteSettings.EmailFrom, SiteSettings.EmailTo); message.Subject = "New Email Subscription to News Letter"; message.Body = $"Email: {newsLetterSubscribeModel.Email} subscribed to the news letter."; SmtpClient client = new SmtpClient(SiteSettings.MailServer) { Port = 587, EnableSsl = true, }; client.Credentials = new NetworkCredential(SiteSettings.GmailUsername, SiteSettings.GmailPassword); try { client.Send(message); } catch (Exception ex) { Log.Error(ex); throw; } } catch (Exception e) { resultModel.Result = false; resultModel.Message = "An error occurred please contact [email protected]"; Log.Error(e); } return(Json(resultModel)); }