How to attach a file with a form data ?

HI

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("http://www.mydomain.com.mx/contact_body_message.aspx?Email=" & 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()
        sr.Close()
        Return result
    End Function 'ScreenScrapeHtml

Open in new window

dimensionavAsked:
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.

CodeCruiserCommented:
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
0
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?

Thanks!
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

dimensionavAuthor Commented:
I asked that because I have understood that the attached file will never be stored on the hosting server, am I right?
0
CodeCruiserCommented:
>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.
0
dimensionavAuthor Commented:
Ok, in that case I shouldn´t be concerned about how to delet it, right?
0
CodeCruiserCommented:
Depends on your application. You should usually delete these files.
0
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
0
Alfred A.Commented:
Try This,

Dim atch as Attachment = New Attachment(Server.MapPath("Somefile.dat"))
Msg.Attachments.Add(atch)
0

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?
0
CodeCruiserCommented:
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.
0
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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.