Send Email using ASP.NET 2.0 and C#

This Example code will show how to send a simple email message with an attachment using ASP.NET 2.0 and C#

Actually it is very simple to send an email with an attachment using ASP.NET 2.0 and C#.

Namespace: you will need to import the System.Web.Mail namespace.

Class: The System.Net.Mail namespace contains the MailMessage and MailAttachment Classes that we need in order to send the email and the message attachment.

We use the btnSubmit_Click event to do the work.

We then call the emailClient.Send to send the message using the variables from our ASP.NET coded page.

The txtAttachmentPath.Text Texbox provides the path to the file to attach to the email message.

Code sendmail.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using System.Net.Mail;
using System.Web.Mail;

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


protected void btnSubmit_Click(object sender, EventArgs e)
MailMessage MyMessage = new MailMessage();

MyMessage.From = txtFrom.Text.Trim();
MyMessage.To = txtTo.Text.Trim();

MyMessage.Subject = txtSubject.Text.Trim();
MyMessage.Body = txtBody.Text.Trim();
MyMessage.Priority = MailPriority.High;

MailAttachment mailAttachments = new MailAttachment(txtAttachmentPath.Text);


litStatus.Text = "Message Sent";
catch (Exception ex)
litStatus.Text = ex.ToString();


sendmail.aspx file:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendMail.aspx.cs" Inherits="Test.SendMail" %>

Untitled Page

SMTP Server

pls find attached code file for your reference.


  • Code zip file for your reference (
  • Comments

    No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: