aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to send mail with attachement in

Dec 2, 2010

In this article I will explain how to send email with attachment using


In previous post I explained how to send mail in now I will explain how to implement mail sending concept with attachment in

To implement this concept first we need to following reference to our application System.Web.Mail namespace 

What is System.Web.Mail

The System.Web.Mail namespace contains classes that enable you to construct and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server.
How we can get this reference (System.Web.Mail

To add this reference follow below steps.

a        a)    On the Project menu, click Add Reference.
b)    On the .NET tab, locate System.Web.dll, and then click Select.
c)    Click OK in the Add References.

After that design your aspx page like this 

<html xmlns="">
<head id="Head1" runat="server">
<title>Send Mail using</title>
<form id="form1" runat="server">
<table style=" border:1px solid" align="center">
<td colspan="2" align="center">
<b>Send Mail with Attachment using</b>
<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
Attach a file:
<asp:FileUpload ID="fileUpload1" runat="server" />
<td valign="top">
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
Now add following namcespace in codebehind

using System.Web.Mail;
After that write the following code in button click

protected void btnSubmit_Click(object sender, EventArgs e)
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = txtFrom.Text;
// Recipient e-mail address.
Msg.To = txtTo.Text;
// Subject of e-mail
Msg.Subject = txtSubject.Text;
if (fileUpload1.HasFile)
// File Upload path
String FileName = fileUpload1.PostedFile.FileName;
//Getting Attachment file
MailAttachment mailAttachment = new MailAttachment(FileName, MailEncoding.Base64);
//Attaching uploaded file

Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpMail.SmtpServer = "";
Msg = null;
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMailWithAttachment.aspx';}</script>");
catch (Exception ex)
Console.WriteLine("{0} Exception caught.", ex);


I hope it helps you

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


goa - developer - trainee said...

awsome man ur super nice.

Dhruval Dave said...

Great Work Sir..

Ajay Patil said...

wel done !!!!!!!!!!

Suresh Dasari said...

thanks guys.......

B2 said...

realy good one...

Anonymous said...

grt one

parthicooldude said...

really grate work .............!!!!!!!!!!
i love this sight..........!

imran said...

I need to take one gridview in that gridview only 2 columns(userid,Role) values need to be display from tblAppUsers_dummy2(table name) ,but for 2nd column(role) I need to take one dropdown,and all roles from the backend should be displayed in gridview behind that edit,delete link buttons there,if I click on edit values from userid,role need to be edited and by clicking on update it should be updated..and by clicking on delete the row should be deleted..
Note: in footer I need to add one more row for userid, that also for userid I need to take one textbox and for role I need to take one dropdown..after I enter values in both the fields by clicking on insert button,the row should be added..and in that row edit,delete link button should be displayed..
.i need this requirement urgently,so pls send me the perfect code..thanks in advance friend..

Anonymous said...

hi i am getting this error
{System.Web.HttpException: Invalid mail attachment 'file123.pdf'.
at System.Web.Mail.MailAttachment.VerifyFile()
at System.Web.Mail.MailAttachment..ctor(String filename, MailEncoding encoding

Anonymous said...

Unknown Error message is displayed

Anonymous said...

I have all the previous steps, but the message does not reach the email I do not know where the problem please help me quickly because I need this code is necessary

Suresh Dasari said...

Please check whether smtp server working or not.

Anonymous said...

i checked smtp and i tried send email but the message not reach

Amit said...

My SMTP is working (checked as telnet 25)
But I am getting the exception such
(1)Invalid mail attachment 'file1.jpg' or 'file2.txt'
(2)The transport failed to connect to the server

Anonymous said...

Sir i'm not getting the System.Web.dll On the Project menu Add Reference. On the .NET tab

plz suggest me what to do....

chandana said...

great job. how clear the concept is. doing great job man

Garan Suthagaran said...

sir i have problem to send a mail ,...
how to configure a remote smtp server,..

Garan Suthagaran said...

sir i want to step by step process to send a mail in

Santosh Gurav said...

i want full e-mail facility in my web site how i will implement?please give me solution....

Anonymous said...

First i will appreciate your work and helping others with your coding thanks to that.
i am new to .net coding but, i want to learn and interested to do coding. I don't know what is smtp and your mention in u r coding that smtp server (IP Address) may i know from where i can get.Thanks

Surendra Singh said...

nice coding dear

Anonymous said...

first i want appreciate for tour work.
i executed this code. but mail is not coming to my mail. please can u help for me.

Anonymous said...

ur all article are nice i am working on wcf i have created wcf services but i want to know how can i deploy it on iis and consume it from there i am using vs2010 and window 7 .please give me ur kind respond asap

Anonymous said...

Hi sir, am using vs2005 in Add Reference i dont hav System.Web.dll....... Pls reply how to add System.web.dll

vijay nelson said...
This comment has been removed by the author.
vijay nelson said...
This comment has been removed by the author.
vijay nelson said...

it doesnot show the message about wheather its sent or not..

Susanta said...

very nice..

ANIL BABU Mandla said...

I am getting an error

GOPI said...

Shows Warning!! like this

System.Web.Mail.MailEncoding is Obsolete:

Ssai kiran said...

how to which class & method used for mail attachment in
(system.web.mail---> Mail Attachment class is there )

Sathyan G said...

Invalid mail attachment 'uoms.xlsx'.

am getting this error when i click submit button.

Avik Mukherjee said...

thank you for this valuable post well done.

venky venkatesh said...

hello sir,
am no getting any exception or error but mail is not sending..can you plz suggest me what to do.

HammathuMuneerul huda said...

how to get our remote SMTP server IP.

Nag Arjun said...

It is very useful.................

Anonymous said...


Anonymous said...

this is waste not working...........

Anonymous said...

thnx its really nice..

Umesh Rangani said...

Hi Sir
Plz Help Me
Your Demo Is Good But I Cant Send Mail In Another Pc By IP As Given In Demo So Plz Help Me

alok_smart said...

awesome one problem though how to handle this in n-tier environment Page->BAL->DAL

Anonymous said...

Hello sir, i am following ur tutorial regularly and also referring others to this site, now my issue was i dont get system.web.dll in .net tab. please help me

Yatan Sahni said...

I have a mailbox in ...and i want to send mail on muliple numbers which is present in textbox with semicolon. how to store the numbers in database ??

Pugal Kannan said...
This comment has been removed by the author.
Veersh Dachepally said...

Hai Suresh,
I want to Send A mail With Attachment but In the Attachment I need to Procide Some Details Like Username,Phnum,Etc,,,,,,
My Question is ?
How to Create a attachment At run time And How to Send to a User.?
Please Any One Can Help

Thanks In Advance

Anonymous said...

i can't find in .NET tab, System.Web.dll i am using VS 2010Express.

Srikanth Reddy said...

how to create a aatachment at inside the gmail account only popup dont download option plz solve the problem

Ankit Menaria said...

i m getting error of namespace what should i do...should i use namespace System.Net.Mail

Somiya Agarwal said...

This code is not working....

It shows attached file as Invalid Attachment..

Dattatray salunkhe said...

Sorry code is not working properly ...... :(

dinez kumar said...
This comment has been removed by the author.
dinez kumar said...

How to Download gmail attachments in our webpage

divi said...

hai frds i cant add dll in my am download and add System.web.dll .after i cant see that reference name in soultion expol.. then smtp server address ?how to find smtp server? reply me qk

Unknown said...

thank you sir it worked

Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.