Sunday, July 10, 2011

How to sned mail using gmail SMTP in c#

string subject = "gmail test ";
        string message = "<h3>test sending gmail message content in HTML body</h3>";
        MailMessage mailMsg = new MailMessage();
        mailMsg.From = new System.Net.Mail.MailAddress("sender@gmail.com", "display name", System.Text.Encoding.UTF8);
        mailMsg.To.Add(new MailAddress("reciver@gmail.com"));
        mailMsg.SubjectEncoding = System.Text.Encoding.UTF8;
        mailMsg.BodyEncoding = Encoding.UTF8;
        //
        mailMsg.Subject = subject;
        mailMsg.Body = message;
        mailMsg.IsBodyHtml = true;
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
        // user name and password to SMTP
        client.Credentials = new System.Net.NetworkCredential("sender", "1234");
        //gmail smtp port: 587
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        // gmail required SSL
        client.EnableSsl = true;
        client.Send(mailMsg);