How To Send Email Through Gmail In ASP.NET

  1. Create a web application in VS.NET
  2. Add a button control on the Page
  3. Import namespace System.Mail
  4. Write down following code in the click event of the button
protected void SendMail()
        //Create Mail Message Object with content that you want to send with mail.
        MailMessage MyMailMessage = new MailMessage
                          ("", "",
                          "This is the mail subject", "Just wanted to say Hello");
        MyMailMessage.IsBodyHtml = false;
        //Proper Authentication Details need to be passed when sending email from gmail
        NetworkCredential mailAuthentication = new NetworkCredential("", "myPassword");
        //Smtp Mail server of Gmail is "" and it uses port no. 587
        //For different server this details changes and you can
        //get it from respective server.
        SmtpClient mailClient = new SmtpClient("", 587);
        //Enable SSL
        mailClient.EnableSsl = true;
        mailClient.UseDefaultCredentials = false;
        mailClient.Credentials = mailAuthentication;


Post a Comment


Close Menu