Click here to Skip to main content
15,392,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi after clcikcing the file upload control can anybody please help me in how to send a mail to someperson incluidng that attachment
Updated 25-Dec-13 17:55pm
ravikiran from Hyderabad 26-Dec-13 0:04am
Details required?

You have a webserver or what?

you can send Emails using gmail also.
spanner21 26-Dec-13 0:07am
yes i am having my default server to there i will be semding mail from my controls like textbox texta and fileupoadcontrols after sending mail to that server it will automatically send to the users who needs that mail ..

Please follow this link. It will help you lot.[^]

Or take a look at this ,
 Here’ I’ll explain how to attach files to email without saving them on the disk

You can refer my articles on how to send emails in

Send SMTP Emails using System.Net Class in C#   and Send SMTP Emails using System.Net Class in VB.Net

When the File is uploaded using FileUpload Control the file is received in the httppostedfile


To attach the HttpPostedFile  to the email. You will need to convert it to Stream.

The code below attaches a posted file to email. Note that txtAttachment is  a FileUpload control

<asp:FileUpload ID="txtAttachment" runat="server" />


if (txtAttachment.PostedFile != null)




        string strFileName =


        Attachment attachFile =

        new Attachment(txtAttachment.PostedFile.InputStream, strFileName);







spanner21 26-Dec-13 0:05am
HI bhavesh it is showing file not found at that link
BK 4 code 26-Dec-13 0:26am
Please see my updated solution
Two Situations

1. You are running your website in Host.

2. You are running your website in Another machine(Client).


Using different browsers you will get problem

(suppose chrome displays only filename insted of path.)


eg: In your client machine filepath (C:\asdf.txt)
server you may not contain C:\asdf.txt file.

Better thing upload file into Server.
and send from that path.
delete file after sent email.

(suggesition use different program for delete).
Copy file to server.
Attach to mail.
Send mail.
Delete file after sending.
use namespaces
using System.Net.Mime;
using System.Text;
using System.Net.Mail;
using System;

and method

private Boolean SendMailUsingGmail(String mailTo, String mailText, String attachmentFileWithPath)
     Boolean sendMailResult;
         SmtpClient smtpServer = new SmtpClient();
         smtpServer.Credentials = new System.Net.NetworkCredential(mailFromId, mailFromPassword);
         smtpServer.Port = 25;
         smtpServer.Host = "";
         MailMessage sampleMail = new MailMessage();
         sampleMail.From = new MailAddress(mailFromId);
         sampleMail.Subject = "Mail";
         string file = attachmentFileWithPath; //file path
         Attachment data = new Attachment(file, MediaTypeNames.Application.Zip);
         ContentDisposition disposition = data.ContentDisposition;
         disposition.CreationDate = System.IO.File.GetCreationTime(file);
         disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
         disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
         // Add the file attachment to this e-mail message.
         sampleMail.Body = mailText;
         sendMailResult = true;
         return sendMailResult;
     catch (Exception ex)
         string exceptionString = ex.Message;
         sendMailResult = false;
         return sendMailResult;


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900