public ActionResult Submit(SubmitWtfViewModel model) { if (!this.ModelState.IsValid) { return(View(model)); } var recaptchaHelper = this.GetRecaptchaVerificationHelper(); if (string.IsNullOrEmpty(recaptchaHelper.Response)) { this.ModelState.AddModelError(string.Empty, "Captcha answer cannot be empty."); return(View(model)); } var recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse(); if (recaptchaResult != RecaptchaVerificationResult.Success) { this.ModelState.AddModelError(string.Empty, "Invalid recaptcha response: " + recaptchaResult); return(View(model)); } try { using (var writer = new StringWriter()) using (var smtp = new SmtpClient(Config.Wtf.Mail.Host)) using (var message = new MailMessage(InedoLib.Util.CoalesceStr(model.SubmitForm.Email, Config.Wtf.Mail.FromAddress), Config.Wtf.Mail.ToAddress)) { WriteCommonBody(writer, model.SubmitForm.Name, model.SubmitForm.NameUsage); switch (model.SubmitForm.Type) { case SubmissionType.CodeSod: message.Subject = "[Code]"; WriteCodeSodBody(writer, model.SubmitForm.Language, model.SubmitForm.CodeSnippet, model.SubmitForm.Background); AttachFile(message, model.SubmitForm.CodeFile); break; case SubmissionType.Story: message.Subject = "[Story]"; WriteStoryBody(writer, model.SubmitForm.StoryComments); break; case SubmissionType.Errord: message.Subject = "[Error'd]"; WriteErrordBody(writer, model.SubmitForm.ErrordComments); AttachFile(message, model.SubmitForm.ErrordFile); break; default: throw new InvalidOperationException("Invalid submission type"); } message.Subject = message.Subject + " " + model.SubmitForm.Title; message.Body = writer.ToString(); smtp.Send(message); } return(View(new SubmitWtfViewModel { ShowLeaderboardAd = false, SuccessMessage = "Your submission was sent, thank you!" })); } catch (Exception ex) { model.ErrorMessage = ex.Message; return(View(model)); } }
public async Task <ActionResult> Submit(SubmitWtfViewModel model) { if (this.ModelState.IsValid) { await this.CheckRecaptchaAsync(); } if (!this.ModelState.IsValid) { return(View(model)); } try { using (var writer = new StringWriter()) using (var message = new MailMessage(new MailAddress(Config.Wtf.Mail.FromAddress, model.SubmitForm.Name), new MailAddress(Config.Wtf.Mail.ToAddress))) { message.ReplyToList.Add(model.SubmitForm.Email); WriteCommonBody(writer, model.SubmitForm.Name, model.SubmitForm.NameUsage, model.SubmitForm.Email); long tag; string title; var attachments = new KeyValuePair <string, HttpContent> [0]; switch (model.SubmitForm.Type) { case SubmissionType.CodeSod: tag = AsanaClient.CodeSodTagId; title = "[CodeSOD] "; WriteCodeSodBody(writer, model.SubmitForm.Language, model.SubmitForm.CodeSnippet, model.SubmitForm.Background); attachments = AttachFile(message, model.SubmitForm.CodeFile, true); break; case SubmissionType.Story: tag = AsanaClient.StoryTagId; title = "[Story] "; WriteStoryBody(writer, model.SubmitForm.StoryComments); break; case SubmissionType.Errord: tag = AsanaClient.ErrordTagId; title = "[Error'd] "; WriteErrordBody(writer, model.SubmitForm.ErrordComments); attachments = AttachFile(message, model.SubmitForm.ErrordFile, true); break; default: throw new InvalidOperationException("Invalid submission type"); } title += model.SubmitForm.Title; message.Subject = title; message.Body = writer.ToString(); _ = AsanaClient.Instance.CreateTaskAsync(tag, title, writer.ToString(), attachments); await this.SendMailAsync(message); } return(View(new SubmitWtfViewModel { ShowLeaderboardAd = false, SuccessMessage = "Your submission was sent, thank you!" })); } catch (Exception ex) { model.ErrorMessage = ex.Message; return(View(model)); } }