Introduction
Send emails using Simple Mail Transfer Protocol (SMTP) with attachments.
Requirement
Namespace: using System.Net.Mail;
Source code
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Send Mail using asp.net</title>
- <style type="text/css">
- .auto-style1 {
- color: #FFFFFF;
- font-weight: bold;
- background-color: #3399FF;
- }
- .auto-style2 {
- background-color: #3399FF;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table style="border-style: solid; border-color: inherit; border-width: 1px; background-color: #49A3FE;" align="center">
- <tr>
- <td colspan="2" align="center" class="auto-style1">Send Mail with Attachment using asp.net
- </td>
- </tr>
- <tr>
- <td class="auto-style1">From:
- </td>
- <td>
- <asp:TextBox ID="txt_from" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td class="auto-style1">To:
- </td>
- <td>
- <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td class="auto-style1">Subject:
- </td>
- <td>
- <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td class="auto-style1">Attach a file:
- </td>
- <td>
- <asp:FileUpload ID="fileUpload1" runat="server" />
- </td>
- </tr>
- <tr>
- <td valign="top" class="auto-style1">Body:
- </td>
- <td>
- <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td class="auto-style2"></td>
- <td>
- <asp:Button ID="btnSubmit" Text="Send" runat="server" OnClick="btnSubmit_Click" Style="height: 26px" />
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Design
Code behind- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Net.Mail;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- MailMessage mail = new MailMessage();
- mail.To.Add(txtTo.Text);
-
-
- mail.From = new MailAddress(txt_from.Text);
- mail.Subject = txtSubject.Text;
- mail.Body = txtBody.Text;
- mail.IsBodyHtml = true;
-
-
- if (fileUpload1.HasFile)
- {
- mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
- }
- SmtpClient smtp = new SmtpClient();
- smtp.Host = "smtp.gmail.com";
- smtp.Credentials = new System.Net.NetworkCredential
- ("your email id", "your password");
-
- smtp.EnableSsl = true;
- smtp.Send(mail);
- }
- }
RunPress "Ctrl + S" or save all the work then click "F5" to run the page. The page looks as in the following images:
Using this form you can send emails with attachments.