Sending Email with Attachment using ASP.Net

  1. <table border="0" cellpadding="0" cellspacing="0">  
  2.     <tr>  
  3.         <td style="width: 80px">  
  4.             To:  
  5.         </td>  
  6.         <td>  
  7.             <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>  
  8.         </td>  
  9.     </tr>  
  10.     <tr>  
  11.         <td>  
  12.                
  13.         </td>  
  14.     </tr>  
  15.     <tr>  
  16.         <td>  
  17.             Subject:  
  18.         </td>  
  19.         <td>  
  20.             <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>  
  21.         </td>  
  22.     </tr>  
  23.     <tr>  
  24.         <td>  
  25.                
  26.         </td>  
  27.     </tr>  
  28.     <tr>  
  29.         <td valign = "top">  
  30.             Body:  
  31.         </td>  
  32.         <td>  
  33.             <asp:TextBox ID="txtBody" runat="server" TextMode = "MultiLine" Height = "150" Width = "200"></asp:TextBox>  
  34.         </td>  
  35.     </tr>  
  36.     <tr>  
  37.         <td>  
  38.                
  39.         </td>  
  40.     </tr>  
  41.     <tr>  
  42.         <td>  
  43.             File Attachment:  
  44.         </td>  
  45.         <td>  
  46.             <asp:FileUpload ID="fuAttachment" runat="server" />  
  47.         </td>  
  48.     </tr>  
  49.     <tr>  
  50.         <td>  
  51.                
  52.         </td>  
  53.     </tr>  
  54.     <tr>  
  55.         <td>  
  56.             Gmail Email:  
  57.         </td>  
  58.         <td>  
  59.             <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>  
  60.         </td>  
  61.     </tr>  
  62.     <tr>  
  63.         <td>  
  64.                
  65.         </td>  
  66.     </tr>  
  67.     <tr>  
  68.         <td>  
  69.             Gmail Password:  
  70.         </td>  
  71.         <td>  
  72.             <asp:TextBox ID="txtPassword" runat="server" TextMode = "Password"></asp:TextBox>  
  73.         </td>  
  74.     </tr>  
  75.     <tr>  
  76.         <td>  
  77.                
  78.         </td>  
  79.     </tr>  
  80.     <tr>  
  81.         <td></td>  
  82.         <td>  
  83.             <asp:Button Text="Send" OnClick="SendEmail" runat="server" />  
  84.         </td>  
  85.     </tr>  
  86. </table>  
  1. using System.IO;  
  2. using System.Net;  
  3. using System.Net.Mail;  
  4.   
  5. protected void SendEmail(object sender, EventArgs e)  
  6. {  
  7.     using(MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))   
  8.     {  
  9.         mm.Subject = txtSubject.Text;  
  10.         mm.Body = txtBody.Text;  
  11.         if (fuAttachment.HasFile)   
  12.         {  
  13.             string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);  
  14.             mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));  
  15.         }  
  16.         mm.IsBodyHtml = false;  
  17.         SmtpClient smtp = new SmtpClient();  
  18.         smtp.Host = "smtp.gmail.com";  
  19.         smtp.EnableSsl = true;  
  20.         NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);  
  21.         smtp.UseDefaultCredentials = true;  
  22.         smtp.Credentials = NetworkCred;  
  23.         smtp.Port = 587;  
  24.         smtp.Send(mm);  
  25.         ClientScript.RegisterStartupScript(GetType(), "alert""alert('Email sent.');"true);  
  26.     }  
  27. }