Wednesday, August 18, 2010

Sending Emails using Gmail User Account in Asp.Net

To send an email you can use System.Net.MailMessage class.  The following example demonstrates how to send an email using your own Gmail user account.

MailMessage mailMsg = new MailMessage();

mailMsg.From =new MailAddress( yourmail@domain.com);
mailMsg.To.Add( jobinjohn@live.com);
mailMsg.Subject = "Hai";
mailMsg.Body = "Chumma";
mailMsg.Priority = MailPriority.High;
SmtpClient c = new SmtpClient("smtp.gmail.com",587); // or u can use port 465
NetworkCredential cred = new NetworkCredential("username", "password"); //ur account username and password to authenticate your request.
c.Credentials = cred;
c.EnableSsl = true;
try
{
c.Send(mailMsg);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}

No comments: