ASP.NET (35) SQL (25) JAVASCRIPT (24) HTML (14) STYLE SHEET (6) ASP (4) SCRIPT (1)

Search me out HERE!!

To Send Mail in ASP.NET With C# using GMAIL Account

Below code can be used in ASP.net with C# to send email using your Gmail Account.

string from = me@gmail.com; //Replace this with your own correct Gmail Address
string to = to@abc.com //Replace this with the Email Address to whom you want to send the mail

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from, "Admin" , System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ;
mail.Priority = MailPriority.High;

//Add the Creddentials- use your own email id and password
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(from, "Password");

client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
    client.Send(mail);
}
catch (Exception ex)
{
    HttpContext.Current.Response.Write(ex2.ToString());
} // end try

No comments:

Post a Comment