Sending Email From Asp.Net Page

By Atul Chavan - 6:15 PM

To send A email from asp.net page using SMTP protocol follow following steps:

  • In web.config add following code
    <system.net>

    <mailSettings>

      <smtp>
        <network host="smtp.gmail.com"

         port="587"

         userName="your mail id"

         password="password"

         enableSsl="true"/>

      </smtp>

    </mailSettings>

  </system.net>
</configuration>


  •  Add a C# function to send email
                        
 protected void sendMail()
    {
        try
        {
            MailMessage mailMessage = new MailMessage()
            {
                Subject = contactSubject.Text,

                Body = contactBody.Text ,

                From = new MailAddress(contactEmail.Text),

                IsBodyHtml = true
            };


            mailMessage.To.Add(new MailAddress("atul39.chavan@gmail.com"));

            SmtpClient smtpClient = new SmtpClient();

            smtpClient.SendAsync(mailMessage, mailMessage);

            lblStatus.Visible = true;

            lblStatus.Text = "Your Request Submitted Successfully ..!!";
         }

        catch
        {
            lblStatus.Text = "Sorry Try Again Later..!!";
        }
    }

  • Call the above created function on any of the event such as button click, page load etc.

                            


  • Share:

You Might Also Like

0 comments