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.
<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>
|
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);
}
}
|
|
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 Email
|
|||
|
|
Subscribe by RSS
Subscribe by Email
29 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