Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send email from Asp.net Mvc-3?

How to send a mail through mvc-3 asp.net using c#?

I have to send a forgot password so how can I do this? My code is below.

Model code..

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations;  namespace TelerikLogin.Models.ViewModels {     public class ForgotPassword     {         public int user_id { get; set; }         public string user_login_name { get; set; }         public string user_password { get; set; }          [Required]         [Display(Name="Email Address : ")]         public string user_email_address { get; set; }     } } 

Controller code..

  public ActionResult ForgotPassword()         {             return View();         }          [HttpPost]         public ActionResult ForgotPassword(string user_email_address)         {             SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\MVC3\TelerikLogin\TelerikLogin\App_Data\Login.mdf;Integrated Security=True;User Instance=True");              DataTable dt1 = new DataTable();              string strQuery = string.Format("SELECT user_password FROM [user_master] WHERE user_email_address='{0}'",user_email_address);             conn.Open();             SqlDataAdapter da1 = new SqlDataAdapter(strQuery, conn);             da1.Fill(dt1);             conn.Close();              if (dt1.Rows.Count > 0)             {  MailMessage msg = new MailMessage();              msg.From = new MailAddress("[email protected]");             msg.To.Add(user_email_address);             msg.Subject = "Password";             msg.Body = "Test1";             msg.Priority = MailPriority.High;              SmtpClient client = new SmtpClient();                 client.Credentials = new NetworkCredential("[email protected]", "dip", "smtp.gmail.com");             client.Host = "smtp.gmail.com";             client.Port = 587;             client.DeliveryMethod = SmtpDeliveryMethod.Network;             client.EnableSsl = true;             client.UseDefaultCredentials = true;              client.Send(msg);                  return RedirectToAction("About", "Home");             }             return View();         } 

Here I fetched the password of user from database through entered email address..

View code..

<% using (Html.BeginForm("ForgotPassword", "Account", FormMethod.Post))    { %>     <%: Html.LabelFor(m => m.user_email_address) %>    <%: Html.TextBox("user_email_address")%>       <%: Html.ValidationSummary(true) %>  <input type="submit" value="Submit"/>     <%} %> 

It gives me an error on these line

 client.Send(msg); 

Error messege is:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. x1sm1264662igc.16 

How to solve it? thanks in advance

like image 214
Dip Avatar asked Mar 09 '12 06:03

Dip


People also ask

Can C# send an email?

The standard approach to send an email using C# is SMTP (Simple Mail Transfer Protocol). It is a network protocol used to send emails over the internet. Additionally, it allows you to relayemails across multiple networks.


1 Answers

Import the System.Net.Mail namespace.

The code will look similar to this:

MailMessage mail = new MailMessage();  SmtpClient smtpServer = new SmtpClient("smtp.gmail.com"); smtpServer.Credentials = new System.Net.NetworkCredential("userName", "password"); smtpServer.Port = 587; // Gmail works on this port  mail.From = new MailAddress("[email protected]"); mail.To.Add("[email protected]"); mail.Subject = "Password recovery"; mail.Body = "Recovering the password";  smtpServer.Send(mail); 

P.S. You have a SQL injection vulnerability in the sample code. Use a SqlCommand object with parameters instead of String.Format().

Using SqlDataReader would be a lot more efficient to check for a record instead of populating a DataSet.

like image 171
Dmitry S. Avatar answered Oct 04 '22 11:10

Dmitry S.