Home > Code > ASP.NET > Sending Mails with Attachements using Asp.Net

Sending Mails with Attachements using Asp.Net

by Narain Siddharth   on Nov 13, 2010   Category: ASP.NET   |  Views: 4756    |  Points: 25   |  Silver 

Below is the sample code for sending emails with attachments.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Net.Mail;
using System.Text;

namespace TestWeb.aspdotnet
public partial class mail : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
string strSMTP = "SMTP Server Name";
int strPort = 25; //Default 25
MailMessage msg = new MailMessage("from Mail-ID", "To Mail-ID");
msg.CC.Add("CC mail-ID");// If necessary
msg.Body = "Test Mail";
msg.Subject = "Test Subject";
msg.BodyEncoding = Encoding.ASCII;
msg.IsBodyHtml = true;
msg.SubjectEncoding = Encoding.UTF8;
//Attachment Part
Attachment attach1 = new Attachment(@"Path");//Attachment Path
SmtpClient smtp = new SmtpClient(strSMTP);
System.Net.NetworkCredential nc = new System.Net.NetworkCredential("UserName", "Pwd", "Domain");
smtp.Credentials = (System.Net.ICredentialsByHost)nc.GetCredential(strSMTP, strPort, "Basic");
catch (Exception ex)


If you find this above code useful, please leave your comments.

Post Code  |  Code Snippet Home

User Responses

  Re :Sending Mails with Attachements using Asp.Net   
Posted by Spidy
on 10/03/2011
Points : 5

very useful code.
Can you post code for sending emails with GMAIL account using asp.netm if possible.


Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts