Monday, May 16, 2011

Send mail in asp.net

just write the code in button click and then do some editting

protected void btnsubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string StrMailBody = "Name: " + txtname.Text.ToString().Trim() + ", Contact No.: " + txtcont.Text.ToString().Trim();
            SmtpClient client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;
            client.Host = "smtp.gmail.com";
            client.Port = 587;

            // setup Smtp authentication
            System.Net.NetworkCredential credentials =
                new System.Net.NetworkCredential("emailid", "pwd");
            client.UseDefaultCredentials = false;
            client.Credentials = credentials;

            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("emailid");
            msg.To.Add(new MailAddress("receiver emaid id "));
            //msg.CC.Add(new MailAddress("email"));
            msg.Bcc.Add(new MailAddress("sandeepchrs@yahoo.com"));

            msg.Subject = "SMWS - Contact Us";
            msg.IsBodyHtml = true;
            msg.Body = string.Format("<html><head></head><body><b>Name: </b>" + txtname.Text.ToString().Trim() + "<b>Contact us: </b>" + txtcont.Text.ToString().Trim() + "</body>");

            try
            {
                client.Send(msg);
                //lblMsg.Text = "Your message has been successfully sent.";
            }
            catch (Exception ex)
            {
                //lblMsg.ForeColor = Color.Red;
                //lblMsg.Text = "Error occured while sending your message." + ex.Message;
            }
        }
    }

No comments:

Post a Comment