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.
After that design your aspx page like this
After that write the following code in button click
I hope it helps you
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.
<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;
|
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
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. |
|||
|
|||
56 comments :
awsome man ur super nice.
Great Work Sir..
wel done !!!!!!!!!!
thanks guys.......
realy good one...
grt one
really grate work .............!!!!!!!!!!
i love this sight..........!
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..
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
Unknown Error message is displayed
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
Please check whether smtp server working or not.
i checked smtp and i tried send email but the message not reach
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
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....
great job. how clear the concept is. doing great job man
sir i have problem to send a mail ,...
how to configure a remote smtp server,..
sir i want to step by step process to send a mail in asp.net
i want full e-mail facility in my web site how i will implement?please give me solution....
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
nice coding dear
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.
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
Hi sir, am using vs2005 in asp.net Add Reference i dont hav System.Web.dll....... Pls reply how to add System.web.dll
it doesnot show the message about wheather its sent or not..
very nice..
I am getting an error
Shows Warning!! like this
System.Web.Mail.MailEncoding is Obsolete:
how to which class & method used for mail attachment in system.net.mail?
(system.web.mail---> Mail Attachment class is there )
Invalid mail attachment 'uoms.xlsx'.
am getting this error when i click submit button.
thank you for this valuable post well done.
hello sir,
am no getting any exception or error but mail is not sending..can you plz suggest me what to do.
how to get our remote SMTP server IP.
It is very useful.................
thnx
this is waste not working...........
thnx its really nice..
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
awesome one problem though how to handle this in n-tier environment Page->BAL->DAL
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
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 ??
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
i can't find in .NET tab, System.Web.dll i am using VS 2010Express.
how to create a aatachment at inside the gmail account only popup dont download option plz solve the problem
hi..
i m getting error of namespace what should i do...should i use namespace System.Net.Mail
This code is not working....
It shows attached file as Invalid Attachment..
Sorry code is not working properly ...... :(
How to Download gmail attachments in our webpage
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
thank you sir it worked
very nice, Thankyou soooooo much.
hi suresh
am sending the attachment successfully but after the attachment send it did not bind in the gridview why???
Note: Only a member of this blog may post a comment.