Sending Email From Asp.Net Page

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.

                            


Sending Email From Asp.Net Page Sending Email From Asp.Net Page Reviewed by Atul Chavan on 6:15 PM Rating: 5
Powered by Blogger.