Monday, September 22, 2008

Sending Email Using ASP.NET

There are two way to do that
1 using Localhost.
2 using gmail emailid and password

But I feel second methode more beneficial because it works fine when we install our software or website on server.
First one mostly gives errors.


The main code

  ///////////////////////////////Main code to send the email


 try
 {
 SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("***@gmail.com", "abcd");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //this is required

//Builed The MSG
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add("***@yahoo.co.in");//whom you want to send
msg.From = new MailAddress(TextBox1.Text , "USER", System.Text.Encoding.UTF8);
msg.Subject = "subject";
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = "\n\n\nUser:       " + TextBox1.Text + "\n\n\n\n" + "Message:     \n\n" + TextBox2.Text;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false;
msg.Priority = MailPriority.High;

try
 {
 client.Send(msg);
  }
  catch (Exception ex)
  {
 //WRITE THE MESSAGE TO SHOW AT THE TIME OF ERROR
 }
}
 catch (Exception ex)
 {
  //WRITE THE MESSAGE TO SHOW AT THE TIME OF ERROR           
 }
 }


////If you feel any problem then post your reply.

No comments: