i made contact form has no error not sending email main purpose of page? sending email using smtp gmail. password hidden in code please change before try. confused why isn't working.i have tried lot in vain. working in mvc 5 think there problem related it.
//model class code starts........................................... using system; using system.collections.generic; using system.linq; using system.web; using system.text; using system.componentmodel.dataannotations; namespace contacts.models { public class contact { [required] [display(name = "name *")] public string name { get; set; } [required] [datatype(datatype.emailaddress)] [display(name = "email address *")] public string email { get; set; } [datatype(datatype.phonenumber)] [display(name = "phone number")] public string phone { get; set; } [required] [display(name = "message *")] public string body { get; set; } public datetime sentdate { get; set; } public string ip { get; set; } public string buildmessage() { var message = new stringbuilder(); message.appendformat("date: {0:yyyy-mm-dd hh:mm}\n", sentdate); message.appendformat("email from: {0}\n", name); message.appendformat("email: {0}\n", email); message.appendformat("phone: {0}\n", phone); message.appendformat("ip: {0}\n", ip); message.appendformat("message: {0}\n", body); return message.tostring(); } } } model class code end ............................. controller class code starts ............................. using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using contacts.models; using system.net; using system.net.mail; using system.collections; namespace contacts.controllers { public class contactcontroller : controller { public actionresult index() { viewbag.success = false; return view(new contact()); } [httppost] public actionresult index(contact contact) { viewbag.success = false; { try { if (modelstate.isvalid) { // collect additional data; contact.sentdate = datetime.now; contact.ip = request.userhostaddress; smtpclient smtpclient = new smtpclient(); smtpclient.usedefaultcredentials = false; smtpclient.credentials = new system.net.networkcredential ("tehmina.diya@gmail.com", "********"); smtpclient.enablessl = true; mailmessage mail = new mailmessage(); mail.from = new mailaddress("tehmina.diya@gmail.com"); // mail.to.add(new mailaddress("tehmina.diya@gmail.com")); // mail.subject = "your email subject"; // subject mail.body = contact.buildmessage(); contact.buildmessage(); // body viewbag.success = true; smtpclient.send(mail); } } catch (exception e) { response.write("success"); } } return view(); } } } controller class code ends ............................. view class code starts............................. @model contacts.models.contact @{ viewbag.title = "contact page"; } <h2> contact page</h2> <script src="@url.content("~/scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@url.content("~/scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> @html.validationsummary(true, "sending message unsuccessful. please correct errors , try again.") @if (viewbag.success) { <h3>your message sent!</h3> } @using (html.beginform()) { <div> <fieldset> <legend>contact information</legend> <div class="editor-label"> @html.labelfor(model => model.name) </div> <div class="editor-field"> @html.textboxfor(model => model.name) @html.validationmessagefor(model => model.name) </div> <div class="editor-label"> @html.labelfor(model => model.email) </div> <div class="editor-field"> @html.textboxfor(model => model.email) @html.validationmessagefor(model => model.email) </div> <div class="editor-label"> @html.labelfor(model => model.phone) </div> <div class="editor-field"> @html.textboxfor(model => model.phone) @html.validationmessagefor(model => model.phone) </div> <div class="editor-label"> @html.labelfor(model => model.body) </div> <div class="editor-field"> @html.validationmessagefor(model => model.body) <br /> <textarea rows="10" cols="60" name="body"></textarea> </div> <p> <input type="submit" value="send" /> </p> </fieldset> </div> } //view class ends....................
here great link in stackoverflow deals smtp , gmail.
sending email through gmail smtp server c#
anyway, got code work. there several minor errors in mvc code.
in controller, in catch block, have
catch (exception e) { response.write("success"); }
change to
catch (exception e) { modelstate.addmodelerror("",e.message); }
this adds error receive gmail model state. catch block catches errors, not success!
then in view, have following line of code
@html.validationsummary(true, "sending message unsuccessful. please correct errors , try again.")
change to:
@html.validationsummary(true, "", new { @class = "text-danger" })
this display error message catch in catch block if transmission fails.
now, go controller , before following line
smtpclient.send(mail)
and add following code
smtpclient.host = "smtp.gmail.com"; smtpclient.port = 587; smtpclient.deliverymethod = system.net.mail.smtpdeliverymethod.network; smtpclient.timeout = 20000;
so looks this
smtpclient.host = "smtp.gmail.com"; smtpclient.port = 587; smtpclient.deliverymethod = system.net.mail.smtpdeliverymethod.network; smtpclient.timeout = 20000; smtpclient.send(mail)
it should work. if start timeouts, increase timeout in code.
Comments
Post a Comment