asp.net mvc - How to send email from MVC 5 application -
i have form client required fill out. 1 time form submitted, i'd send basic info form's index view (first name, lastly name, phone number, etc..) email. i'm using godaddy hosting site. matter, or can send email straight mvc application? have next model, view, controller. i've never done before , not sure how go it.
model:
public class application { public int id { get; set; } [displayname("marital status")] public bool? maritalstatus { get; set; } [required] [displayname("first name")] public string firstname { get; set; } [displayname("middle initial")] public string middleinitial { get; set; } [required] [displayname("last name")] public string lastname { get; set; } }
controller:
public actionresult index() { homecoming view(); } // post: applications/create // protect overposting attacks, please enable specific properties want bind to, // more details see http://go.microsoft.com/fwlink/?linkid=317598. [httppost] [validateantiforgerytoken] public actionresult index([bind(include = "id,firstname,middleinitial,lastname")] application application) { viewbag.submitdate = datetime.now; if (modelstate.isvalid) { application.getdate = datetime.now; db.applications.add(application); db.savechanges(); homecoming redirecttoaction("thanks"); } homecoming view(application); }
view
<table class="table table-striped"> <tr> <th> @html.actionlink("first name", "index", new { sortorder = viewbag.namesortparm }) </th> <th> @html.actionlink("last name", "index", new { sortorder = viewbag.namesortparm }) </th> <th> @html.actionlink("date submitted", "index", new { sortorder = viewbag.namesortparm}) </th> </tr> @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.firstname) </td> <td> @html.displayfor(modelitem => item.lastname) </td> <td> @html.displayfor(modelitem => item.getdate) </td> </tr> }
you need smtp server send email from. no thought how godaddy works i'm sure provide something.
to send emails mvc app either specify smtp details in code or in web.config
. recommend in config file means it's much easier change. in web.config:
smtpclient client=new smtpclient();
otherwise, in code:
smtpclient client=new smtpclient("some.server.com"); //if need authenticate client.credentials=new networkcredential("username", "password");
now create message:
mailmessage mailmessage = new mailmessage(); mailmessage.from = "someone@somewhere.com"; mailmessage.to.add("someone.else@somewhere-else.com"); mailmessage.subject = "hello there"; mailmessage.body = "hello friend!";
finally send it:
client.send(mailmessage);
an illustration web.config
set up:
<system.net> <mailsettings> <smtp> <network host="your.smtp.server.com" port="25" /> </smtp> </mailsettings> </system.net>
asp.net-mvc sendmail
No comments:
Post a Comment