Send Mail in Forget Password Using Config File in Asp.net

Send Mail in Forget Password Using Config File in Asp.net

Creating SMTP in Web Config File

<configuration>
<connectionStrings>
<add name=”Connection” connectionString=”Data Source=.\SQLExpress;Initial Catalog=practise;Integrated Security=True;” providerName=”System.Data.SqlClient”/>
</connectionStrings>
<system.net>
<mailSettings >
<smtp>
<network host=”smtp.gmail.com” port=”587″ userName=”Email Id” password=”Password” />
</smtp>
</mailSettings>
</system.net>
<appSettings>
<add key=”MailFrom” value=”Email Id”/>
</appSettings>
</configuration>
//Call this Function From where u want to sen Email using App Settings in Config File

public bool ForgetEmail(string UserName)
{
string Connection = ConfigurationManager.ConnectionStrings[“Connection”].ToString();
SqlConnection con = new SqlConnection(Connection);
con.Open();

SqlCommand command = new SqlCommand(“SELECT Password from UserTable where email='” + UserName + “‘ “, con);
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[“Table”].Rows.Count > 0)
{

SmtpClient sm = new SmtpClient();
MailMessage msg = new MailMessage();

string msgfromadd = ConfigurationManager.AppSettings[“MailFrom”];
MailAddress frmadd = new MailAddress(msgfromadd);
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.From = frmadd;
msg.To.Add(EmailID);
msg.Subject = “Your Password.”;
msg.Body = “Your password is:”+” “+ds.Tables[“Table”].Rows[0][0].ToString();
msg.IsBodyHtml = true;
con.Close();
sm.EnableSsl = true;

sm.Send(msg);
return true;
con.Close();
}
else
{
return false;
con.Close();
}
}

1 Comment

Leave a Reply