Send email to recepient email address stored in database in ASP.Net

on Feb 22, 2013 04:28 AM

actually i am developing a web application for leave,,if the employee wants to take a leave,the employee working under the senior people,if the employee 001 wants to take  a leave,if the employee 001 logins in the web application,if he is working in sales dept,automatically this mail is forwarded to sales wise for all the employee..if the employee works in accounts dept,whenever the employee logins in the web application,to email address automatically change to the accounts manager..i dont no how to do this..can you  tell me .how to send the mail..i am not trying to send the mail in gmail..can anyone tell me how to do this?

Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Feb 25, 2013 03:24 AM
on Feb 25, 2013 03:25 AM

This is the sample project which i have created and the below is the aspx code

 <form id="form1" runat="server">
        <asp:DropDownList ID='ddlEmployeeId' runat="server">
        <asp:Button ID="btnSendEmail" runat="server" Text="Send Email" OnClick="OnSendEmailClicked" />


protected void Page_Load(object sender, EventArgs e)
            ddlEmployeeId.DataSource = GetData("SELECT ecode,empname FROM Leaves");
            ddlEmployeeId.DataTextField = "empname";
            ddlEmployeeId.DataValueField = "ecode";

        protected void OnSendEmailClicked(object sender, EventArgs e)
            DataTable dataTable = new DataTable();
            dataTable = GetData("SELECT ecode,empname,Department,pl,sl,lop,totdays,depthead FROM Leaves WHERE ecode='" + ddlEmployeeId.SelectedItem.Value + "'");
            string recepientEmail = dataTable.Rows[0]["depthead"].ToString();
            string body = "<br /><br />Leave Application<br /> EmpCode: " + dataTable.Rows[0]["ecode"].ToString() + "<br /> Employee Name: " + dataTable.Rows[0]["empName"].ToString() + "<br />Department: " + dataTable.Rows[0]["Department"].ToString() + "<br />PL: " + dataTable.Rows[0]["pl"].ToString() + "<br />SL: " + dataTable.Rows[0]["sl"].ToString() + "<br />LOP: " + dataTable.Rows[0]["lop"].ToString() + "<br />Total Days: " + dataTable.Rows[0]["totdays"].ToString() + "";
            string subject = "Leave Application";
            SendEmail(recepientEmail, subject, body);

        private DataTable GetData(string query)
            string connectionString = ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;
            DataTable dataTable = new DataTable();
            SqlConnection sqlconnection = new SqlConnection();
            sqlconnection.ConnectionString = connectionString;
            SqlCommand sqlCommand = new SqlCommand(query, sqlconnection);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            return dataTable;

        public static void SendEmail(string recepientEmail, string subject, string body)
            using (MailMessage mm = new MailMessage())
                string fromEmailAddress = "";
                mm.From = new MailAddress(fromEmailAddress);
                mm.To.Add(new MailAddress(recepientEmail));
                mm.Subject = subject;
                mm.Body = body;
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "";
                smtp.EnableSsl = true;
                System.Net.NetworkCredential networkCred = new System.Net.NetworkCredential();
                networkCred.UserName = fromEmailAddress;
                networkCred.Password = "your gmail password";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = networkCred;
                smtp.Port = 587;

You will also have to use this NameSpace

using System.Net.Mail;

and check whether it works