Sending Email With an Attachment in asp.net using c#
In this tutorial you will learn how to send attachment with email in asp.net using c#. To send an email with attachments, the ASP.NET process (or the ASP.NET impersonated account) will need permission to read the file, and attach it to the MailMessage class. You can attach a file using FileUpload Control and put the file in memory stream. In this example I'm using smtp.gmail.comas my SMTP server. You can put your gmail login credentials and send mail with attachment.
In .aspx file
Sample screen:
In .aspx.cs file
In .aspx file
<table style="padding-left: 10px; background-color: #4B8DF8; color: #fff; font-weight: bold;"> <tr> <td colspan="2"> <span class="primary"><b>Mail With Attachment:</b></span> </td> </tr> <tr> <td style="width: 30%"> From : </td> <td> <asp:TextBox ID="txtFrom" SkinID="textbox_larger" runat="server"></asp:TextBox> </td> </tr> <tr> <td> To : </td> <td> <asp:TextBox ID="txtTo" SkinID="textbox_larger" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Subject : </td> <td> <asp:TextBox ID="txtSubject" SkinID="textbox_larger" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Message : </td> <td> <asp:TextBox ID="txtMessage" TextMode="MultiLine" SkinID="textbox_multiline_smaller" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Attach : </td> <td> <asp:FileUpload ID="FileUpload1" runat="server" /> </td> </tr> <tr> <td> </td> <td align="left"> <asp:Button ID="btnSendMail" runat="server" SkinID="button_primary" OnClick="btnSendMailWithAttachment_Click" Text="Send Mail"></asp:Button> </td> </tr> </table>
Sample screen:
In .aspx.cs file
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Linq; using System.Net.Mail; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSendMailWithAttachment_Click(object sender, EventArgs e) { MailMessage mail = new MailMessage(); mail.To.Add(txtTo.Text); mail.From = new MailAddress(txtFrom.Text); mail.Subject = txtSubject.Text; mail.Body = txtMessage.Text; mail.IsBodyHtml = true; //Attach file using FileUpload Control and put the file in memory stream if (FileUpload1.HasFile) { mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName)); } SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address smtp.Credentials = new System.Net.NetworkCredential ("Your gmail id", "your gmail password"); //Or your Smtp Email ID and Password smtp.EnableSsl = true; smtp.Send(mail); } }
You can also send emails with attachments in c# with Aspose.Email for .NET Library. It offers many other features like creating,reading, editing and converting emails to other formats. Try it:
ReplyDeletehttp://www.aspose.com/.net/email-component.aspx