Send Email using CSharp.Net

Posted by Venkat | Labels: ,

Now we have to discuss how to send email using GMAIL server.

Normally we can use , Gmail, Yahoo , etc.. some company having own mailserver so you have to contact your Admin to know your mailserver , id , pwd etc..

This is code to send email using Asp.net user port no is 587 or 465 it depends upon system

declare the port below the smtp server name

smtp.port=587

using System.Net.Mail;


//Now write this code in click event of button

//C# code

protected void Button1_Click(object sender, EventArgs e)
{
  MailMessage mail = new MailMessage();
  mail.To.Add("admin@gmail.com");
  mail.From = new MailAddress("admin@gmail.com");
  mail.Subject = "Email using Gmail";

  string Body = "Hi, this mail is to test sending mail"+
                "using Gmail in ASP.NET using C#";
  mail.Body = Body;

  mail.IsBodyHtml = true;
  SmtpClient smtp = new SmtpClient();
  smtp.Host = "smtp.gmail.com"; 
  smtp.EnableSsl = true; 
  smtp.Credentials = new System.Net.NetworkCredential
       ("YourUserName@gmail.com","YourGmailPassword");
    smtp.Send(mail);
}

//you have to give your Gmail Username and password ..

0 comments:

Post a Comment

Thanks for the Comments.

PayOffers.in