Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add attachments to email?

Status
Not open for further replies.

GEAK

Instructor
Feb 1, 2001
90
US
Nutshell: How do I get the value from <INPUT type="file"...> into the code behind a webform?

Detailed: I'm writing a web-based email client (a la hotmail, gmail, yahoo...). I know how to send mail already (been there, done that, got the stamps on my passport). I want to allow the client to browse for a file and attach that to the mail message. I'm probably overlooking the obvious but I can't seem to figure out how to get the value from a generic HTML control (input type=file) on my webform to the point where I can use it to create a MailAttachment object.

BTW, I'm a programming instructor but 99% of my experience is using C++ to write stand-alone apps, this VB/ASP.Net stuff is new to me.
 
YOUR ASPX PAGE
<input type=file runat=server id=file1 />
<asp:button runat=server id=butSubmit text=Submit/>

YOUR CODE BEHIND
Private Sub butSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSubmit.Click

If Not file1.postedfile.filename = Nothing Then
file1.postedfile.saveas("MyFilePathAndName.txt")
EmailObject.attachments.add("MyFilePathAndName.txt")
'Alternative
Dim ln As Integer = file1.PostedFile.ContentLength - 1
Dim bytes(ln) As Byte
file1.PostedFile.InputStream.Read(bytes, 0, ln)
EmailObject.attachments.add(bytes)
End If
End Sub

Keeping it Simple
 
Thanks a pile! Turns out the missing link wasn't in what you gave me, but you pointed me in the right direction. I originally declared my input as an HtmlInputControl and couldn't access any useful properties without throwing exceptions. Once I changed it to HtmlInputFile everything fell into place.
 
Generally what I do is have a function that will return a boolean value if the file that is being uploaded passes certain validation. e.g.
Code:
    Public Function FileFieldSelected(ByVal FileField As System.Web.UI.HtmlControls.HtmlInputFile) As Boolean
        ' Returns a True if the passed
        ' FileField has had a user post a file
        Dim intFileLength As Integer
        If FileField.PostedFile Is Nothing Then Return False
        If FileField.PostedFile.ContentLength = 0 Then Return False
        If FileField.PostedFile.ContentLength > 5120000 Then Return False
        Return True
    End Function
Then, I simply pass the HTML control to the function and if it is valid save the file. e.g.
Code:
        If FileFieldSelected(fileUpload1) = True Then
            fileUpload1.PostedFile.SaveAs("PathToSaveAs")
        End If


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top