How to attach a file with a form data ?


I would like to send the data from a form by mail (this is not a big deal) but in the form will be a field that will ask for a PDF file in order to "attach it" to the message, considering that I wonder the following:

1. I need the code for attach the file to an email, I use System.Net.Mail.MailMessage for that purpose with an special function (please see below code area).
2. Would be possible that each time that somebody sends a messages, part of the code make a clean up in order to avoid have stored alot of PDF files in the server ?
3. Would be possible to protect the selection of the file for only PDF files ?

Thanks in advance.
' This is how I call de function
        Dim Messagebody As String = ScreenScrapeHtml("" & tbEmail.Text & "&Name=" & tbName.Text & "&Address=" & tbAddress.Text & "&Subject=" & ddlSubject.SelectedValue & "&Comments=" & tbComments.Text)

'This is the funcion
    Public Function ScreenScrapeHtml(ByVal url As String) As String
        Dim objRequest As Net.WebRequest = System.Net.HttpWebRequest.Create(url)
        Dim sr As New IO.StreamReader(objRequest.GetResponse().GetResponseStream())
        Dim result As String = sr.ReadToEnd()
        Return result
    End Function 'ScreenScrapeHtml

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

1) Mailmsg.Attachments.Add(New Attachment("filepath")
2) You can File.Delete(filename) after sending the mail message.
3) If the file is uploaded from the client side, you need to perform the validation on the server. Use the IO.Path.GetExtension method to get the extension and compare with .pdf
dimensionavAuthor Commented:
I´ve been reading your documentation, I just have a doubt, How to get the file path from the client machine, which controls and objects?

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

dimensionavAuthor Commented:
I asked that because I have understood that the attached file will never be stored on the hosting server, am I right?
>I have understood that the attached file will never be stored on the hosting server
The file is being uploaded from the client? It will have to be stored on the server somewhere even though it would be temporary.
dimensionavAuthor Commented:
Ok, in that case I shouldn´t be concerned about how to delet it, right?
Depends on your application. You should usually delete these files.
dimensionavAuthor Commented:
Thanks CodeCruiser, finally could you tell me how to get the client path and name of the file that will be attached ?

Thanks for everything
Alfred A.Commented:
Try This,

Dim atch as Attachment = New Attachment(Server.MapPath("Somefile.dat"))

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dimensionavAuthor Commented:
I have a form that collects all data that will be sent (Name, Company, etc.) even the file that will be attached, do I have to use FileUpload control in order to get the file name from client?
Yes. You can not access the file on client on the server side without uploading it to the server. So you have to use the fileupload control to upload the file on a temp location on the server, attach the file, send message, and then delete the file.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.