Wednesday 2 May 2012

Send Mail From Gmail in C#

Write this small code for sending e-Mail from your Gmail account

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add("Destination Mail Address");
mail.From = new MailAddress("UserName", "One and only",
                                        System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail again";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "body Of Message";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("UserName",
                                                                           "Password");
client.Port = 587;  // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Send(mail);
Enjoy.............

No comments:

Post a Comment