Aspdotnet-Suresh

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

how to send mail with attachement in asp.net

Dec 2, 2010
Introduction:

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

Description:

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

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="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Send Mail using asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style=" border:1px solid" align="center">
<tr>
<td colspan="2" align="center">
<b>Send Mail with Attachment using asp.net</b>
</td>
</tr>
<tr>
<td>
From:
</td>
<td>
<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
To:
</td>
<td>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Attach a file:
</td>
<td>
<asp:FileUpload ID="fileUpload1" runat="server" />
</td>
</tr>
<tr>
<td valign="top">
Body:
</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
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)
{
try
{
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.Attachments.Add(mailAttachment); 
}

Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpMail.SmtpServer = "10.120.0.21";
SmtpMail.Send(Msg);
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);
}
}

Demo



I hope it helps you

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

Get the latest Asp.net, C#.net, 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

56 comments :

goa - developer - trainee said...

awsome man ur super nice.

IamDD said...

Great Work Sir..

Ajay Patil said...

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

Suresh Dasari said...

thanks guys.......

B2 said...

realy good one...

Anonymous said...

grt one

Anonymous 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,role..in 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....

Anonymous said...

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

Unknown said...

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

Unknown said...

sir i want to step by step process to send a mail in asp.net

Unknown said...

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

Anonymous said...

hi,
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

UI Design said...

nice coding dear

Anonymous said...

hi,
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 asp.net Add Reference i dont hav System.Web.dll....... Pls reply how to add System.web.dll

FirstProgrammer said...
This comment has been removed by the author.
FirstProgrammer said...
This comment has been removed by the author.
FirstProgrammer said...

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

Susanta said...

very nice..

Help 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.net.mail?
(system.web.mail---> Mail Attachment class is there )

Anonymous 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.

Venkatesh Ekkaluri said...

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

Unknown said...

how to get our remote SMTP server IP.

Suggest Me Bro said...

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

Anonymous said...

thnx

Anonymous said...

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

Anonymous said...

thnx its really nice..

Unknown 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

Unknown said...

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

Pugal 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.

Unknown said...

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

Unknown said...

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

Unknown said...

This code is not working....

It shows attached file as Invalid Attachment..

Unknown said...

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

Unknown said...
This comment has been removed by the author.
Unknown said...

How to Download gmail attachments in our webpage

divi said...

hai frds i cant add dll in my website.so 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

Unknown said...

very nice, Thankyou soooooo much.

Anonymous said...

hi suresh
am sending the attachment successfully but after the attachment send it did not bind in the gridview why???

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

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