Wednesday, 4 January 2012

Simple code to Send a mail by using Smtp.Host as gmail.com

Coding Part:-
use namespace:
using System.Net.Mail;

protected void btnSendmail_Click(object sender, EventArgs e)
{
try
{

MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
mail.From = new MailAddress(txtEmail.Text);
// mail.To.Add(new MailAddress(txtTo.Text));
mail.To.Add(new MailAddress("shibashishXYZ@gmail.com"));

mail.Subject = txtSubject.Text;
mail.Body =txtName.Text + txtMessage.Text;
mail.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com";


//smtp.Host = "localhost";

smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "txtEmail.Text";
NetworkCred.Password = "1234";
//NetworkCred.UserName = "xyz@shibashish.com";
// NetworkCred.Password = "Password123!";
smtp.UseDefaultCredentials = true;
//smtp.Credentials = NetworkCred;
smtp.Credentials = new System.Net.NetworkCredential("txtEmail.Text", "1234");

smtp.Port = 587;//this is Gmail port for e-mail
//smtp.Port = 25;// this is default port no
smtp.Send(mail);//send an e-mail

lblStatus.Text="Message Sended";
}
catch(Exception ex)
{
throw ex;
}
}


Design View:-


Contact Form




Please enter the following requested
information below to send us your Message.





 


No comments:

Post a Comment