Skip to main content

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
                          ("test@gmail.com", "myfriend@domain.com",
                          "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("test@gmail.com", "myPassword");
        //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
        //For different server this details changes and you can
        //get it from respective server.
        SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
        //Enable SSL
        mailClient.EnableSsl = true;
        mailClient.UseDefaultCredentials = false;
        mailClient.Credentials = mailAuthentication;
        mailClient.Send(MyMailMessage);

    }

Comments

Popular posts from this blog