---
title: "Send Uploaded File as attachment"  
description: "Send Uploaded File as attachment"  
author: "Anonymous User"  
published: 2014-08-25  
updated: 2014-08-25  
canonical: https://www.mindstick.com/forum/2125/send-uploaded-file-as-attachment  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Send Uploaded File as attachment

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to get the [uploaded file](https://www.mindstick.com/forum/137/how-to-check-uploaded-file-type-using-asp-dot-net) to be sent as an attachment in my ashx file, Here is the [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) I am using

```
        HttpPostedFile fileupload = context.Request.Files[0];        //filename w/o the path        string file = Path.GetFileName(fileupload.FileName);        string saveLocation = HttpContext.Current.Server.MapPath("UploadDirName") + "\\" + file;        fileupload.SaveAs(saveLocation);                   MailMessage message = new MailMessage();        //*****useless stuff********        message.To.Add("abc@xxx.com");        message.Subject = "test";        message.From = new MailAddress("test@aaa.com");        message.IsBodyHtml = true;        message.Body = "testing";         //*****useless stuff********        //Fault line        message.Attachments.Add(new Attachment(saveLocation, MediaTypeNames.Application.Octet))        //Send mail        SmtpClient smtp = new System.Net.Mail.SmtpClient("xxxx", 25);        smtp.UseDefaultCredentials = false;        smtp.Credentials = new NetworkCredential("xxx", "xxxx");        smtp.Send(message);
```

I am able to send the [email](https://www.mindstick.com/articles/12870/10-easy-ways-to-manage-your-business-email-inbox) w/o the attachment.

Do I need to save the file first? and then [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) to attachment ?

I haven't worked much on [form](https://www.mindstick.com/forum/6/multi-form-mfc-application) [processing](https://www.mindstick.com/blog/254/background-processing-in-android) in asp.net.

## Replies

### Reply by Sumit Kesarwani

Hi John, try this:

```
HttpPostedFile file =Request.Files["upload_your_file"];if (file != null && file.ContentLength > 0){    string fileName =Path.GetFileName(file.FileName);    var attachment =new Attachment(file.InputStream, fileName);    mailMessage.Attachments.Add(attachment);}
```


---

Original Source: https://www.mindstick.com/forum/2125/send-uploaded-file-as-attachment

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
