Pages

Thursday, March 10, 2011

Send mail through gmail smtp


Following snippet is for sending mail through gmail smtp. create 4 textboxes for to, from,subject & message

            System.Net.Mail.MailMessage mailmessage=new System.Net.Mail.MailMessage();
            mailmessage.Body = txtBody.Text;
            mailmessage.From = new System.Net.Mail.MailAddress( txtFrom.Text);
            mailmessage.To.Add(txtTo.Text);
            mailmessage.Subject = txtSubject.Text;
            mailmessage.IsBodyHtml = true;
            System.Net.Mail.SmtpClient smt = new System.Net.Mail.SmtpClient();
            smt.UseDefaultCredentials = false;

            smt.Credentials = new System.Net.NetworkCredential("username@gmail.com", "pwd");
            smt.Host = "smtp.gmail.com";
            smt.Port = 587;
            smt.EnableSsl = true;

            smt.Send(mailmessage);