Send File Attachment in Email in ASP.Net Using C#

We took one field and one fileupload control and one button. On button click the mail body and the attached file are sent to the respective email.

Initial Chamber

Step 1

Open your Visual Studio 2010 and create an empty website, provide a suitable name [Email_demo].

Step 2

In Solution Explorer you get your empty website, add a web form as in the following.

For Web Form:

Email_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it Email_demo.aspx.

Design Chamber

Next we will create some design for our application where we drag some control from the toolbox. So open your Email.aspx page and write code like the following.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <style type="text/css">  
  9.         .style1  
  10.         {  
  11.             width: 106px;  
  12.         }  
  13.         .style2  
  14.         {  
  15.             text-decoration: underline;  
  16.             font-size: large;  
  17.             width: 257px;  
  18.         }  
  19.         .style3  
  20.         {  
  21.             width: 257px;  
  22.         }  
  23.     </style>  
  24. </head>  
  25. <body>  
  26.     <form id="form1" runat="server">  
  27.     <div>  
  28.       
  29.         <table style="width:100%;">  
  30.             <tr>  
  31.                 <td class="style1">  
  32.                      </td>  
  33.                 <td class="style2">  
  34.                     <strong>Email File Attachment using ASP.Net C#</strong></td>  
  35.                 <td>  
  36.                      </td>  
  37.             </tr>  
  38.             <tr>  
  39.                 <td class="style1">  
  40.                      </td>  
  41.                 <td class="style3">  
  42.                      </td>  
  43.                 <td>  
  44.                      </td>  
  45.             </tr>  
  46.             <tr>  
  47.                 <td class="style1">  
  48.                     Email:</td>  
  49.                 <td class="style3">  
  50.                     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  51.                 </td>  
  52.                 <td>  
  53.                     <asp:RequiredFieldValidator ID="RequiredFieldValidator1"   
  54.                         ControlToValidate="TextBox1" runat="server"   
  55.                         ErrorMessage="Please Provide Email ID" Font-Bold="True" ForeColor="Red"></asp:RequiredFieldValidator>  
  56.                   </td>  
  57.             </tr>  
  58.             <tr>  
  59.                 <td class="style1">  
  60.                     Attachment:</td>  
  61.                 <td class="style3">  
  62.                     <asp:FileUpload ID="FileUpload1" runat="server" />  
  63.                 </td>  
  64.                 <td>  
  65.                      </td>  
  66.             </tr>  
  67.             <tr>  
  68.                 <td class="style1">  
  69.                     Body:</td>  
  70.                 <td class="style3">  
  71.                     <asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine"></asp:TextBox>  
  72.                 </td>  
  73.                 <td>  
  74.                      </td>  
  75.             </tr>  
  76.             <tr>  
  77.                 <td class="style1">  
  78.                      </td>  
  79.                 <td class="style3">  
  80.                      </td>  
  81.                 <td>  
  82.                      </td>  
  83.             </tr>  
  84.             <tr>  
  85.                 <td class="style1">  
  86.                     <asp:Label ID="lbmsg" runat="server"></asp:Label>  
  87.                 </td>  
  88.                 <td class="style3">  
  89.                     <asp:Button ID="Button1" runat="server" onclick="Button1_Click"   
  90.                         Text="Send Mail" />  
  91.                 </td>  
  92.                 <td>  
  93.                      </td>  
  94.             </tr>  
  95.         </table>  
  96.       
  97.     </div>  
  98.     </form>  
  99. </body>  
  100. </html>  
Your design will look like this:

design

Code Chamber

Finally open your Email_demo.aspx.cs page where we write our server-side code so that the mail is sent to the intended user with the file attachment. But before that look out the namespace that we will include.

namespace

These two namespaces you need to include in your application and anyhow if you can't find these namespaces in the Intellisense then add them from the references.

The following is the code for Email_demo.aspx.cs:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. using System.Net;  
  9. using System.Net.Mail;  
  10.   
  11.   
  12. public partial class _Default : System.Web.UI.Page  
  13. {  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.   
  17.     }    
  18.     protected void Button1_Click(object sender, EventArgs e)  
  19.     {  
  20.         MailMessage msg = new MailMessage();  
  21.         msg.From = new MailAddress("[email protected]");  
  22.         msg.To.Add(TextBox1.Text);  
  23.              msg.Subject = "Email File attachment";  
  24.             msg.Body = TextBox2.Text;  
  25.             msg.IsBodyHtml = true;  
  26.   
  27.   
  28.         if (FileUpload1.HasFile)  
  29.         {  
  30.             msg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));  
  31.         }  
  32.   
  33.         SmtpClient smt = new SmtpClient();  
  34.         smt.Host = "smtp.gmail.com";  
  35.         System.Net.NetworkCredential ntwd = new NetworkCredential();  
  36.         ntwd.UserName = "[email protected]"//Your Email ID  
  37.         ntwd.Password = ""// Your Password  
  38.         smt.UseDefaultCredentials = true;  
  39.         smt.Credentials = ntwd;  
  40.         smt.Port = 587;  
  41.         smt.EnableSsl = true;  
  42.         smt.Send(msg);  
  43.         lbmsg.Text = "Email Sent Successfully";  
  44.         lbmsg.ForeColor = System.Drawing.Color.ForestGreen;  
  45.   
  46.     }  
  47. }  
Output Chamber

Output

I had selected this image file, I'll attach this file to my email, let's see.

Select file

send mail

email file attachment

I hope you like this. Thank you for reading. Have a good day!

 

Up Next
    Ebook Download
    View all
    Learn
    View all