Wednesday, March 6, 2013

How to send mail using Gmail SMTP in .net Windows application(C#)




Step 1:

Create Five Textboxs  and Button




Step 2:

Please type the code in send button click event


private void btnSend_Click(object sender, EventArgs e)
        {

            try
            {
                MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(txtUserName.Text);
                // Recipient e-mail address.
                  Msg.To.Add(txtTo.Text);
                  Msg.Subject = txtSubject.Text;

                                       
                    // your remote SMTP server IP.
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.Credentials = new System.Net.NetworkCredential(txtUserName.Text, txtPassword.Text);
                    smtp.EnableSsl = true;
                    smtp.Send(Msg);
                    Msg = null;
                    MessageBox.Show("Mail sent");
                    // Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='Default.aspx';}</script>");
                }
            
            catch (Exception ex)
            {
                Console.WriteLine("{0} Exception caught.", ex.Message);
            }
        }

Step 3:

                 Run the Program





Message Will Send

No comments:

Post a Comment

Comment Here..