Thursday, March 10, 2011

Send email through gmail

Following snippet is for sending mail through gmail smtp server.

create 4 textboxes for to, from,subject & message. Dot Net has provided us a class System.Net.Mail.SmtpClient. with the help of this class we can send the email.

If your company have an email server you require to change following:
Gmails SMTP port is 587.
Gmails Host address is
Give your username and password("", "pwd") to make following code workable.

            System.Net.Mail.MailMessage mailmessage=new System.Net.Mail.MailMessage();
            mailmessage.Body = txtBody.Text;
            mailmessage.From = new System.Net.Mail.MailAddress( txtFrom.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("", "pwd");
            smt.Host = "";
            smt.Port = 587;
            smt.EnableSsl = true;


Above code snippet will help us to send an email.

Happy Coding !! 

No comments:

Post a Comment