how to send mail with 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.com as my SMTP server. You can put your gmail login credentials and send mail with attachment.
In .aspx file
Sample screen:
In .aspx.cs file
Please share this post if it's useful to you. Thanks!.
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); } }
Please share this post if it's useful to you. Thanks!.
2 comments:
nice article. if you are maintaining any template for email.. this may help you.
http://aspnettutorialonline.blogspot.com/2012/05/email-template-from-flat-file-or.html
great post
http://csharpektroncmssql.blogspot.com/2012/10/how-to-send-email-with-html-format-in.html
Post a Comment
Share your thoughts here...