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 .
- [HttpPost]
- public ActionResult SendNewMessage()
- {
- try
- {
- Response.StatusCode = 200;
-
- string smtpAddress = ConfigurationSMTP.smtpAdress;
-
-
- int portNumber = ConfigurationSMTP.portNumber;
- bool enableSSL = ConfigurationSMTP.enableSSL;
- string emailTo = Request.Params["to"];
- string subject = Request.Params["subject"];
- StringBuilder body = new StringBuilder();
-
- body.Append("<html><head> </head><body>");
- body.Append("<div style=' font-family: Arial; font-size: 14px; color: black;'>Hi,<br><br>");
- body.Append(Request.Params["message"]);
- body.Append("</div><br>");
-
- 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));
- 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));
- body.Append(string.Format("<span style='font-size:11px;font-family: Arial;color:#40411E;'>Tel: {0}</span><br>", MessageModel.phone));
- body.Append(string.Format("<span style='font-size:11px;font-family: Arial;'><a href=\"web site\">{0}</a></span><br><br>", MessageModel.link));
- body.Append(string.Format("<span style='font-size:11px; font-family: Arial;color:#40411E;'>{0}</span><br>", MessageModel.details));
- body.Append( "</body></html>");
- using (MailMessage mail = new MailMessage())
- {
- mail.From = new MailAddress(ConfigurationSMTP.from);
-
- mail.To.Add(emailTo);
- mail.Subject = subject;
- mail.Body = body.ToString();
-
- mail.IsBodyHtml = true;
-
- string localFileName = "~/Content/TestAttachement.txt";
-
- mail.Attachments.Add(new Attachment(Server.MapPath(localFileName), "application/pdf"));
-
- using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
- {
-
- smtp.Credentials = new NetworkCredential(ConfigurationSMTP.from, ConfigurationSMTP.password);
-
- smtp.EnableSsl = enableSSL;
-
- smtp.Send(mail);
- }
- }
- }
- catch (Exception ex)
- {
-
- Response.StatusCode = 400;
- }
- return null;
- }
- }
Model Class:
- public class ConfigurationSMTP
- {
-
- public static string smtpAdress = "smtp.gmail.com";
- public static int portNumber = 587;
- public static bool enableSSL = true;
-
- public static string from = "[email protected]";
- public static string password = "Pakistan_123";
- }
- public class MessageModel
- {
- public static string adress = "full adress of sender";
- public static string link = "link for your website";
- public static string zip = "90";
- public static string city = "paris";
- public static string phone = "(+33) 06 xxxxxxxx";
- public static string details = "detail detail detail detail detail";
- }
Index View:
- @{
- ViewBag.Title = "Index";
- }
- <script type="text/javascript">
- $(document).ready(function () {
- $("#idFormContact").on("submit", function (e) {
- e.preventDefault();
-
- var url = "/Home/SendNewMessage";
- var formdata = (window.FormData) ? new FormData(this) : null;
- var fdata = (formdata !== null) ? formdata : $form.serialize();
- $("#idSubmitMvt").attr("disabled", true);
- $("#idNotifSuccess").hide();
- $("#idNotifError").hide();
-
- $.ajax({
- type: "POST",
- url: url,
- data: fdata,
- processData: false,
- contentType: false,
- success: function (data) {
- $("#idNotifSuccess").show();
- },
- error: function (xhr, ajaxOptions, thrownError) {
- console.log("Error");
- $("#idNotifError").show();
- }
- });
- });
- });
- </script>
- <div class="row">
- <h2>Contact Form</h2>
- <form class="col col-xs-6" id="idFormContact" >
- <div class="form-group">
- <label>Destination</label>
- <input type="email" class="form-control" name="to" value="" placeholder="Destination Email">
- </div>
- <div class="form-group">
- <label>Subject</label>
- <input type="text" class="form-control" value="Test subject" name="subject" placeholder="Subject">
- </div>
- <div class="form-group">
- <label>Body</label>
- <textarea class="form-control" name="message">Test Message</textarea>
- </div>
- <button type="submit" class="btn btn-primary">Submit</button>
- <br>
- <br>
- <div id="idNotifError" style="display:none" class="alert alert-danger">Fail to send a message</div>
- <div id="idNotifSuccess" style="display:none" class="alert alert-success">Message is sent</div>
- </form>
- </div>
Error screen