---
title: "Sending Email in Asp.Net C#"  
description: "Here I have described how to send email in Asp.Net C#."  
author: "chandu kumar"  
published: 2012-11-21  
updated: 2019-08-19  
canonical: https://www.mindstick.com/articles/1063/sending-email-in-asp-dot-net-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Sending Email in Asp.Net C#

Here I have described how to send email in [Asp.Net C#](https://www.mindstick.com/forum/435/textchange-event-at-server-side-using-javascript-in-asp-dot-net-c-sharp).\

For [sending email](https://www.mindstick.com/forum/134/problem-sending-email-in-asp-dot-net), required two [namespaces](https://www.mindstick.com/forum/155583/what-are-namespaces-in-c-sharp) “System.Net;”,” System.Net.Mail;”. These namespaces have some classes (“MailMessage”,” Attachment”,” [SmtpClient](https://www.mindstick.com/interview/34185/how-do-you-send-an-email-using-smtpclient-in-c-sharp)”,” NetworkCredential”) that help to sending mail. Demo is given in few steps.

Step 1: [create table](https://www.mindstick.com/articles/443/how-to-create-table-in-sql-server) with some values.

```
CREATE DATABASE Email 
USE Email 
CREATE TABLE tblEmailDetails
(
Id INT IDENTITY PRIMARY KEY,
EmailId VARCHAR(200) UNIQUE,
UserName VARCHAR(200)
) 
INSERT INTO tblEmailDetails([EmailId],UserName)
VALUES('memtechlodhi@gmail.com','Manish'),
('praveshsinghfaq@gmail.com','Pravesh Singh')
```

Step 2: Create UI part of demo

```
<fieldset style="width:200px; height:100px">
    <legend>Send bulk emails</legend>
    <asp:Button ID="btnSend" Text="Send" runat="server" onclick="btnSend_Click"/>
</fieldset>
```

Step 3: coding part (.cs file)

```
using System;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Net;
using System.Configuration;
 
public partial class index : System.Web.UI.Page{
    protected void Page_Load(object sender, EventArgs e)    { 
    }    protected void btnSend_Click(object sender, EventArgs e)    {        string conString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
        SqlConnection con = new SqlConnection(conString);
        try
        {
            MailMessage msg = new MailMessage(); 
            SqlCommand cmd = new SqlCommand("select EmailId from tblEmailDetails", con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())            {
                msg.Bcc.Add(dr["EmailId"].ToString());
            }
            msg.From = new MailAddress("your email id ");
            msg.Subject = "Testing email delivery";
            msg.Body = "Hello";
            msg.IsBodyHtml = true;
            Attachment imgAtt = new Attachment(Server.MapPath("~/images/issue.png"));
            imgAtt.ContentId = "issue.png";
            msg.Attachments.Add(imgAtt);
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            NetworkCredential credentials = new NetworkCredential("your email id", "your email password");
            client.Credentials = credentials;
            client.Send(msg);
            Response.Write("We Will Contact you as soon as Possible!!! "); 
        }
        catch { Response.Write("please try again letter!!! "); }
        finally { con.Close(); }
    }
}
```

Note: For sending email, sender (From) required email id and regarding [password](https://www.mindstick.com/blog/118/retriving-user-password-by-using-stored-procedure-in-asp-dot-net). That checks [network](https://yourviews.mindstick.com/view/83910/benefits-of-promotional-posting-on-mindstick-network) credential for [sending emails](https://answers.mindstick.com/qa/109539/how-to-restore-outlook-no-longer-sending-emails) to receiver (To).

---

Original Source: https://www.mindstick.com/articles/1063/sending-email-in-asp-dot-net-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
