Introduction

Send emails using Simple Mail Transfer Protocol (SMTP) with attachments.

Requirement

Namespace: using System.Net.Mail;

Source code

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head id="Head1" runat="server">
  3. <title>Send Mail using asp.net</title>
  4. <style type="text/css">
  5. .auto-style1 {
  6. color: #FFFFFF;
  7. font-weight: bold;
  8. background-color: #3399FF;
  9. }
  10. .auto-style2 {
  11. background-color: #3399FF;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <form id="form1" runat="server">
  17. <div>
  18. <table style="border-style: solid; border-color: inherit; border-width: 1px; background-color: #49A3FE;" align="center">
  19. <tr>
  20. <td colspan="2" align="center" class="auto-style1">Send Mail with Attachment using asp.net
  21. </td>
  22. </tr>
  23. <tr>
  24. <td class="auto-style1">From:
  25. </td>
  26. <td>
  27. <asp:TextBox ID="txt_from" runat="server"></asp:TextBox>
  28. </td>
  29. </tr>
  30. <tr>
  31. <td class="auto-style1">To:
  32. </td>
  33. <td>
  34. <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
  35. </td>
  36. </tr>
  37. <tr>
  38. <td class="auto-style1">Subject:
  39. </td>
  40. <td>
  41. <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
  42. </td>
  43. </tr>
  44. <tr>
  45. <td class="auto-style1">Attach a file:
  46. </td>
  47. <td>
  48. <asp:FileUpload ID="fileUpload1" runat="server" />
  49. </td>
  50. </tr>
  51. <tr>
  52. <td valign="top" class="auto-style1">Body:
  53. </td>
  54. <td>
  55. <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10"></asp:TextBox>
  56. </td>
  57. </tr>
  58. <tr>
  59. <td class="auto-style2"></td>
  60. <td>
  61. <asp:Button ID="btnSubmit" Text="Send" runat="server" OnClick="btnSubmit_Click" Style="height: 26px" />
  62. </td>
  63. </tr>
  64. </table>
  65. </div>
  66. </form>
  67. </body>
  68. </html>
Design

sendmail.png

Code behind
  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. using System.Net.Mail;
  8. public partial class _Default : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. }
  13. protected void btnSubmit_Click(object sender, EventArgs e)
  14. {
  15. MailMessage mail = new MailMessage();
  16. mail.To.Add(txtTo.Text);
  17. //mail.To.Add
  18. mail.From = new MailAddress(txt_from.Text);
  19. mail.Subject = txtSubject.Text;
  20. mail.Body = txtBody.Text;
  21. mail.IsBodyHtml = true;
  22. //Attach file using FileUpload Control and put the file in memory stream
  23. if (fileUpload1.HasFile)
  24. {
  25. mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
  26. }
  27. SmtpClient smtp = new SmtpClient();
  28. smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
  29. smtp.Credentials = new System.Net.NetworkCredential
  30. ("your email id", "your password");
  31. //Or your Smtp Email ID and Password
  32. smtp.EnableSsl = true;
  33. smtp.Send(mail);
  34. }
  35. }
Run

Press "Ctrl + S" or save all the work then click "F5" to run the page. The page looks as in the following images:

sendmail1.png

Using this form you can send emails with attachments.