Friday, January 6, 2012

Sending Email From Your Gmail Account in C#.net

Use these namespace and add function to your event on which you want to send email.
*****************************************************************
using System.Web
using System.Net
using System.Net.Mail
**********************
public void sendml()
{
try
{
SmtpClient client = new SmtpClient();
MailAddress sendto = new MailAddress("email of other person");
MailAddress from = new MailAddress("youremail");
MailMessage message = new MailMessage(from, sendto);
message.IsBodyHtml = true;
message.Subject = "Test by Sandeep";
string st = "Emp Name:" + TextBox1.Text.ToString() + "
Login Time:" + TextBox2.Text.ToString() + "
Logout Time:" + TextBox3.Text + "
Host Name:" + TextBox4.Text + "
Description:" + TextBox5.Text + "";
message.Body = st;
message.IsBodyHtml = true;
NetworkCredential basicauthantication =new NetworkCredential("emailid@gmail.com", "password");
client.Host = "smtp.gmail.com";
client.UseDefaultCredentials = false;
client.Credentials = basicauthantication;
client.EnableSsl = true;
client.Send(message);
Response.Write("mail has been sent");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

No comments: