You want to make a simple WebForm that allows your students
to submit a simple e-mail message with a file attachment to submit a term paper.
Since the attachment is being sent via e-mail to a recipient, there is no need
for you to store the attachment on the file system.
To achieve this, I would simply make a WebForm with the
appropriate fields.
Figure 1 - WebForm for message
<img width=355 height=355 src="/ArticleFiles/913/image001.gif">
The listing does not include any error checking and also
assumes that the student will be uploading a MS Word document. I kept this
short and sweet for demonstration purposes, but it is functional.
Listing 2 - Attaching a file with contentstream
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim msg As New System.Net.Mail.MailMessage()
Dim smtp As New System.Net.Mail.SmtpClient("127.0.0.1")
Dim fromadx As New System.Net.Mail.MailAddress("sample@yourdomain.com", "Term
Paper Mailer")
Dim toadx As New System.Net.Mail.MailAddress("recipient@yourdomain.com",
"Professor Plumb")
msg.From = fromadx
msg.To.Add(toadx)
msg.Subject = txtSubject.Text
msg.Body = txtBody.Text
If FileUpload1.HasFile = True Then
msg.Attachments.Add(New
System.Net.Mail.Attachment(FileUpload1.PostedFile.InputStream, "Final Term
Paper.doc"))
End If
smtp.Send(msg)
End Sub
Another good use for the contentstream attachment is when
the file exists in a byte() array. The byte() data can be put into a System.IO.MemoryStream which is then readable by the
attachment object.
I have used that technique with SQL database image fields.
Listing 3 - MemoryStream example
'Assumes you have a datatable with SQL image data in it.
Dim ms as new System.IO.MemoryStream(Table1.rows(0).item("ImageFieldName"))
The MemoryStream now contains the
contents of the image data from Table1. It can be read by the System.Net.Mail.Attachment object.