Saturday, 11 June 2011

Sending email with attachments in ASP.Net.

Hi friends,in this post I would like to explain how to send email with attachments in Asp.Net.

For this I am taking 3 TextBoxes(For ToAddress,Subject & Body),FileUpload control,label for displaying message and a Button control.

For working with emails you need to add the following namespace:

using System.Net.Mail;

Then code under Send Button Click Event:

protected void btnSendEmail_Click(object sender, EventArgs e)

MailMessage mssg = newMailMessage();

mssg.From = new MailAddress("");


mssg.Subject = txtSubject.Text;

mssg.IsBodyHtml = true;

mssg.Body = txtBody.Text;

mssg.Priority = MailPriority.High;

string strFName = null;

strFName = Path.GetFileName(fulAttach.PostedFile.FileName);

fulAttach.PostedFile.SaveAs(Server.MapPath(strFName ));

Attachment attach = new Attachment(Server.MapPath(strFName ));


System.Net.Mail.SmtpClient client = new system.Net.Mail.SmtpClient();

client.UseDefaultCredentials = false;

client.Credentials = new System.Net.NetworkCredential("", "yourgmailpassword");

client.Port = 587;

client.Host = "";

client.EnableSsl = true;

object userstate = mssg;


lblDisplayMessage.Text = "Mail Sent Successfully.";


Note: where fulAttach is file upload control ID.

Then it will send email along with attachment.

Thank You...

Shout it

One Response so far.

Leave a Reply