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;
- //getting useful configuration
- string smtpAddress = ConfigurationSMTP.smtpAdress;
- //it can be a "smtp.office365.com" or whatever,
- //it depends on smtp server of your sender email.
- int portNumber = ConfigurationSMTP.portNumber; //Smtp port
- bool enableSSL = ConfigurationSMTP.enableSSL; //SSL enable
- string emailTo = Request.Params["to"];
- string subject = Request.Params["subject"];
- StringBuilder body = new StringBuilder();
- //building the body of our email
- body.Append(" ");
- body.Append("Hi,
");- body.Append(Request.Params["message"]);
- body.Append("
"); - //Mail signature
- body.Append(string.Format("{0} - {1} {2}
", MessageModel.adress, MessageModel.zip, MessageModel.city)); - body.Append(string.Format("Mail: {0}
", ConfigurationSMTP.from)); - body.Append(string.Format("Tel: {0}
", MessageModel.phone)); - body.Append(string.Format("{0}
", MessageModel.link)); - body.Append(string.Format("{0}
", MessageModel.details)); - body.Append( "");
- using (MailMessage mail = new MailMessage())
- {
- mail.From = new MailAddress(ConfigurationSMTP.from);
- //destination adress
- mail.To.Add(emailTo);
- mail.Subject = subject;
- mail.Body = body.ToString();
- //set to true, to specify that we are sending html text.
- mail.IsBodyHtml = true;
- // Can set to false, if you are sending pure text.
- string localFileName = "~/Content/TestAttachement.txt";
- //to send a file in attachment.
- mail.Attachments.Add(new Attachment(Server.MapPath(localFileName), "application/pdf"));
- //Specify the smtp Server and port number to create a new instance of SmtpClient.
- using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
- {
- //passing the credentials for authentification
- smtp.Credentials = new NetworkCredential(ConfigurationSMTP.from, ConfigurationSMTP.password);
- //Authentification required
- smtp.EnableSsl = enableSSL;
- //sending email.
- smtp.Send(mail);
- }
- }
- }
- catch (Exception ex)
- {
- //Error response
- Response.StatusCode = 400;
- }
- return null;
- }
- }
Model Class:
- public class ConfigurationSMTP
- {
- //SMTP parameters
- public static string smtpAdress = "smtp.gmail.com";
- public static int portNumber = 587;
- public static bool enableSSL = true;
- //need it for the secured connection
- 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";
- }
- class="row">
Contact Form
- ="col col-xs-6" id="idFormContact" >
class="form-group">- "email" class="form-control" name="to" value="" placeholder="Destination Email">
class="form-group">- "text" class="form-control" value="Test subject" name="subject" placeholder="Subject">
class="form-group">- ="form-control" name="message">Test Message
- class="btn btn-primary">Submit
"idNotifError" style="display:none" class="alert alert-danger">Fail to send a message"idNotifSuccess" style="display:none" class="alert alert-success">Message is sent
Error screen
Bryian TanPosted Apr 19, 2019, 3:43 PM
Laxmidhar SahooPosted Apr 18, 2019, 10:52 AM
Amit KumarPosted Apr 18, 2019, 10:31 AM
Mark TaborPosted Apr 18, 2019, 10:12 AM
Rupesh KahanePosted Apr 18, 2019, 6:37 AM
refer
https://www.c-sharpcorner.com/UploadFile/a8024d/send-event-invitation-emails-using-mvc-5-with-code-first-app/