Use SmtpClient to send email in ASP.NET

Tutorial of SmtpClient in Asp.net

There are several method can help us to send email in .NET. This time, we would like to introduce SmtpClient.
If you do not have a SMTP server, you can use Gmail to do it. SMTP setting of Gmail.

  
    
      
        
      
    
  

Web.config

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendEmail.aspx.cs" Inherits="WebApplication1.SendEmail" %>





    


    

SendEmail.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class SendEmail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSendMail_Click(object sender, EventArgs e)
        {
            SmtpClient client = new SmtpClient();
            MailAddress fromAddress;
            MailAddress toAddress;

            fromAddress = new MailAddress("xxx@email.com", "Coder's Grid");
            toAddress = new MailAddress("xxx@email.com");

            MailMessage msg = new MailMessage(fromAddress, toAddress);
            msg.ReplyToList.Add(fromAddress);

            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Subject = "Testing Mail from Coder's Grid";

            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;

            msg.Body = "

Hello

Welcome to Coder's Grid"; client.Send(msg); } } }

SendEmail.aspx.cs

Below is the result.

Email result

Email

Enjoy.

Related Articles