.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson
Eminent IT

Home >> Code Snippets >> Email >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Sending Mail With Attachment File

Posted By:Athira Sreejith       Posted Date: February 05, 2009    Points: 10    Category: Email    URL: http://www.dotnetspark.com  

Sending a mail with Attachment.

We need to send mail with an attachment below is the sample code to send mail.The code used c# as language

Want to include the following Header

using System.Web.Mail;

string strfrom="";

string strGenericId=Session["GenericId"].ToString();

string strQuery="Update ANC_D_EnduserProblem set EmailStatus='Y' where Logid='"+strGenericId.ToString()+"'";


strsql="select * from UserMaster where UserId=" + Session["UID"].ToString() ;

SqlCommand cmd=new SqlCommand(strsql,objConn);

IDataReader drChapterHeader=cmd.ExecuteReader();

if (drChapterHeader.Read())





MailMessage mail = new MailMessage();

MailAttachment Attachhtml;

Creating a Variable for MailMessage and MailAttachment

The Following line shows from which mail-id we want to send a mail.

mail.From = "bhaskaran@invosystems.com";

The Following line shows to whom we want to send a mail.

mail.To = txtto.Text;

The Following line describes the subject of the mail.

mail.Subject = txtsubject.Text;

string strFileName="";


if(File.Exists(Server.MapPath("DocumentFiles\\wdoc\\" + strcampcode + ".doc"))==true)


The Following line describes how we want to attach a file with the mail.

strFileName=Server.MapPath("DocumentFiles\\wdoc\\" + strcampcode + ".doc");

Attachhtml = new MailAttachment(strFileName);






if(strFileName=="" || strFileName==null)





Attachhtml = new MailAttachment(strFileName);




The Following line describes the body of mail to send

mail.Body = txtbody.Text ;

mail.Priority = MailPriority.High;

mail.BodyFormat = MailFormat.Text;

SmtpMail.SmtpServer = "mail.invosystems.com";

//string mailservername=ConfigurationSettings.AppSettings["mailservername"].ToString();


//SmtpMail.SmtpServer = mailservername;

Sending the Mail



Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend