asp.net mvc 5 - sending mail through controller (using gmail) -


so, first time attempting send mail mvc application , lo , behold, didn't work :(

here's spaghetti i've whipped far sources, needless end in error view:

[httppost] public actionresult contact(contact c) {     if (modelstate.isvalid)     {         try         {             mailmessage msg = new mailmessage();             smtpclient smtp = new smtpclient();             mailaddress = new mailaddress(c.email.tostring());             stringbuilder sb = new stringbuilder();              msg.to.add("myaddress@mail.com");             msg.subject = "new message";             msg.isbodyhtml = false;             smtp.host = "smtp.gmail.com";             smtp.port = 587;             smtp.enablessl = true;             smtp.deliverymethod = smtpdeliverymethod.network;             smtp.credentials = new system.net.networkcredential("myaddress@mail.com", "mypassword");              sb.append("name: " + c.name);             sb.append(environment.newline);             sb.append("email: " + c.email);             sb.append(environment.newline);             sb.append("message: " + c.message);              msg.body = sb.tostring();             smtp.send(msg);             msg.dispose();              return view();         }         catch (exception)         {                 return view("error");         }     }     return view(); } 


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -