Mark Tabor

Mark Tabor

  • 562
  • 1.9k
  • 431.3k

send email using Javascript and MVC

Apr 18 2019 6:32 AM
I follow an article from code project but when i submit the form for email it says 404 bad request
 
below is my code I have method and model class in home controller as shown below .
  1. [HttpPost]  
  2. public ActionResult SendNewMessage()  
  3. {  
  4. try  
  5. {  
  6. Response.StatusCode = 200;  
  7. //getting useful configuration  
  8. string smtpAddress = ConfigurationSMTP.smtpAdress;  
  9. //it can be a "smtp.office365.com" or whatever,  
  10. //it depends on smtp server of your sender email.  
  11. int portNumber = ConfigurationSMTP.portNumber; //Smtp port  
  12. bool enableSSL = ConfigurationSMTP.enableSSL; //SSL enable  
  13. string emailTo = Request.Params["to"];  
  14. string subject = Request.Params["subject"];  
  15. StringBuilder body = new StringBuilder();  
  16. //building the body of our email  
  17. body.Append("<html><head> </head><body>");  
  18. body.Append("<div style=' font-family: Arial; font-size: 14px; color: black;'>Hi,<br><br>");  
  19. body.Append(Request.Params["message"]);  
  20. body.Append("</div><br>");  
  21. //Mail signature  
  22. body.Append(string.Format("<span style='font-size:11px;font-family: Arial;color:#40411E;'>{0} - {1} {2}</span><br>", MessageModel.adress, MessageModel.zip, MessageModel.city));  
  23. body.Append(string.Format("<span style='font-size:11px;font-family: Arial;color:#40411E;'>Mail: <a href=\"mailto:{0}\">{0}</a></span><br>", ConfigurationSMTP.from));  
  24. body.Append(string.Format("<span style='font-size:11px;font-family: Arial;color:#40411E;'>Tel: {0}</span><br>", MessageModel.phone));  
  25. body.Append(string.Format("<span style='font-size:11px;font-family: Arial;'><a href=\"web site\">{0}</a></span><br><br>", MessageModel.link));  
  26. body.Append(string.Format("<span style='font-size:11px; font-family: Arial;color:#40411E;'>{0}</span><br>", MessageModel.details));  
  27. body.Append( "</body></html>");  
  28. using (MailMessage mail = new MailMessage())  
  29. {  
  30. mail.From = new MailAddress(ConfigurationSMTP.from);  
  31. //destination adress  
  32. mail.To.Add(emailTo);  
  33. mail.Subject = subject;  
  34. mail.Body = body.ToString();  
  35. //set to true, to specify that we are sending html text.  
  36. mail.IsBodyHtml = true;  
  37. // Can set to false, if you are sending pure text.  
  38. string localFileName = "~/Content/TestAttachement.txt";  
  39. //to send a file in attachment.  
  40. mail.Attachments.Add(new Attachment(Server.MapPath(localFileName), "application/pdf"));  
  41. //Specify the smtp Server and port number to create a new instance of SmtpClient.  
  42. using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))  
  43. {  
  44. //passing the credentials for authentification  
  45. smtp.Credentials = new NetworkCredential(ConfigurationSMTP.from, ConfigurationSMTP.password);  
  46. //Authentification required  
  47. smtp.EnableSsl = enableSSL;  
  48. //sending email.  
  49. smtp.Send(mail);  
  50. }  
  51. }  
  52. }  
  53. catch (Exception ex)  
  54. {  
  55. //Error response  
  56. Response.StatusCode = 400;  
  57. }  
  58. return null;  
  59. }  
  60. }  
Model Class:
  1. public class ConfigurationSMTP  
  2. {  
  3. //SMTP parameters  
  4. public static string smtpAdress = "smtp.gmail.com";  
  5. public static int portNumber = 587;  
  6. public static bool enableSSL = true;  
  7. //need it for the secured connection  
  8. public static string from = "[email protected]";  
  9. public static string password = "Pakistan_123";  
  10. }  
  11. public class MessageModel  
  12. {  
  13. public static string adress = "full adress of sender";  
  14. public static string link = "link for your website";  
  15. public static string zip = "90";  
  16. public static string city = "paris";  
  17. public static string phone = "(+33) 06 xxxxxxxx";  
  18. public static string details = "detail detail detail detail detail";  
  19. }  
Index View:
  1. @{  
  2. ViewBag.Title = "Index";  
  3. }  
  4. <script type="text/javascript">  
  5. $(document).ready(function () {  
  6. $("#idFormContact").on("submit"function (e) {  
  7. e.preventDefault();  
  8. //call external service  
  9. var url = "/Home/SendNewMessage";  
  10. var formdata = (window.FormData) ? new FormData(this) : null;  
  11. var fdata = (formdata !== null) ? formdata : $form.serialize();  
  12. $("#idSubmitMvt").attr("disabled"true);  
  13. $("#idNotifSuccess").hide();  
  14. $("#idNotifError").hide();  
  15. //get authorization keys.  
  16. $.ajax({  
  17. type: "POST",  
  18. url: url,  
  19. data: fdata,  
  20. processData: false,  
  21. contentType: false,  
  22. success: function (data) {  
  23. $("#idNotifSuccess").show();  
  24. },  
  25. error: function (xhr, ajaxOptions, thrownError) {  
  26. console.log("Error");  
  27. $("#idNotifError").show();  
  28. }  
  29. });  
  30. });  
  31. });  
  32. </script>  
  33. <div class="row">  
  34. <h2>Contact Form</h2>  
  35. <form class="col col-xs-6" id="idFormContact" >  
  36. <div class="form-group">  
  37. <label>Destination</label>  
  38. <input type="email" class="form-control" name="to" value="" placeholder="Destination Email">  
  39. </div>  
  40. <div class="form-group">  
  41. <label>Subject</label>  
  42. <input type="text" class="form-control" value="Test subject" name="subject" placeholder="Subject">  
  43. </div>  
  44. <div class="form-group">  
  45. <label>Body</label>  
  46. <textarea class="form-control" name="message">Test Message</textarea>  
  47. </div>  
  48. <button type="submit" class="btn btn-primary">Submit</button>  
  49. <br>  
  50. <br>  
  51. <div id="idNotifError" style="display:none" class="alert alert-danger">Fail to send a message</div>  
  52. <div id="idNotifSuccess" style="display:none" class="alert alert-success">Message is sent</div>  
  53. </form>  
  54. </div>  
Error screen
 

Answers (5)