Thursday, September 22, 2011

Send Email messages in .Net

The following piece of code explains how to send email messages in .net framework. To do this you need following namespaces.

using System.Net.Mail;

using System.Net;

The following C# coding help to send emails:

private void SendEmail()

{

MailMessage mail = new MailMessage(); //Create MailMessage object

mail.From=new MailAddress("xxxxx@gmail.com"); //set sender mail address

mail.To.Add("xxxxx@gmail.com"); //add set of recipients

mail.Subject = "Subject"; //Subject

mail.Body = "Hi, Im ... Testing NTB mail..."; //Body

var client = new SmtpClient("smtp.gmail.com", 587) //Set email client, email port

{

Credentials=new NetworkCredential("xxxxx@gmail.com", "xxxxxxx"), EnableSsl=true

}; //Senders username, password

client.Send(mail);

mail.Dispose();

}

Cool!!!