Pages

Search This Blog

Thursday 26 June 2014

Code for mail ....

Hi friends, today in this session I will show the code for sending mail.

Mail code was useful to us in many situations like recover password and if you want to send login details to the user after registration.

Here I wrote a code for recover password where the user enters his email id to get username and password to his mail.... 

Here in my code I had took one textbox (to enter user mail id), two label (to specify enter mail and message as ‘sent’ or ‘invalid email’) and one button (to submit mail id)

Here is the code for button onclick function:-

using System.Net.Mail;

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
           DataSet ds = new DataSet();
//Server connection      
 using (SqlConnection con = new SqlConnection("DataSource=SANTOSHKLPKL-HP\\SQLEXPRESS;Initial
Catalog=blood_bank;Integrated Security=True"))
            {

                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT                         USER_NAME,PASSWORD FROM users Where MAIL_ID= '" +  txtEmail.Text.Trim() + "'", con);
            
               SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(txtEmail.Text);
                // Recipient e-mail address.
                Msg.To.Add(txtEmail.Text);
                Msg.Subject = "Your Password Details";
                Msg.Body = "Hi, <br/>Please check your Login Detailss<br/><br/>Your Username: " + ds.Tables[0].Rows[0]["USER_NAME"] + "<br/><br/>Your Password: " + ds.Tables[0].Rows[0]["PASSWORD"] + "<br/><br/>";
                Msg.IsBodyHtml = true;
                // your remote SMTP server IP.
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
             
             // Mail will be sent from the below username          
             smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com""your password");
                
                smtp.EnableSsl = true;
                smtp.Send(Msg);
                
               //Msg = null;
                lbltxt.Text = "Your Password Details Sent to your mail";
               
                // Clear the textbox values & where Textbox id="txtEmail"
                txtEmail.Text = "";
            }
            else
            {
                lbltxt.Text = "The Email you entered not exists.";
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }
   } 

you can rate my class on right side top of my blog ..... 

                                                                         -Your's santoshklpkl

No comments:

Post a Comment

Animated Social Gadget - Blogger And Wordpress Tips Twitter Bird Gadget