Sunday, May 27, 2012

Sending Email using C# and ASP.Net

Introduction
This tip describes how to send email using ASP.NET with C# and using a GMail or Yahoo! port.

Background

This code uses the System.Net.Mail namespace.

Using the code
//Sending Mail Using Gmail

const string FromAddress = "your gmail id";//For Example tejasbhalani@gmail.com
string Subject = TextBox6.Text;
String Body = TextBox4.Text;
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(ToAddress);
mail.From = new MailAddress(FromAddress)
mail.Subject = Subject;
mail.Body = Body;
mail.Priority = System.Net.Mail.MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(FromAddress, "gmail id 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 error)
{
throw error;
}

//Sending Mail Using Yahoo
//send mail using yahoo idstring ToAddress = txtto.Text;
const string FromAddress = "your Yahoo id";//For Example tejasvbhalani@yahoo.com
string Subject = txtsub.Text;
String Body = txtbody.Text;
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(ToAddress);
mail.From = new MailAddress(FromAddress);
mail.Subject = Subject;
mail.Body = Body; client.Credentials = new System.Net.NetworkCredential(FromAddress, "yahoo id password");
mail.Priority = System.Net.Mail.MailPriority.High;
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.mail.yahoo.com";
client.Send(mail);

Use Code For Send Mail to any emailid...........

1 comments:

Post a Comment

Followers