Tutorial 1: Send an Email with an Attachment
Overview | Properties | Methods
The following source code demonstrates how to send an email along with a file attachment via an ASP page.
Files are uploaded by the user via file input elements in a form (with an EncType of "multipart/form-data"). Then the Dundas Upload Control is used to save uploaded files to disk. Once this is done we then iterate through all uploaded files via the Upload control's Files collection and add each file to the Mailer control's Attachments collection. Once all files have been added to this collection the email is sent with the SendMail method of the Mailer control.
Assumptions
A form with an ENCTYPE of "multipart/form-data" is POSTING data to this ASP page.
- The form contains one or more file input elements (e.g. <input type="file" name="txtFile">
<%
Dim objUpload 'stores upload control instance
Dim objEmail 'stores mailer control instance
Dim strPath 'stores path of the asp page
Dim Index 'counter variable
'functions will throw an exception if not successful so On Error Resume Next is used for inline error trapping
On Error Resume Next
Set objUpload = Server.CreateObject("Dundas.Upload") 'Upload object
Set objEmail = Server.CreateObject("Dundas.Mailer") 'Mailer object
'create temporary directory to store uploaded files (if it doesn't already exist)
' at the same directory level as this asp page
strPath = Server.MapPath(".") & "\temp\"
objUpload.DirectoryCreate strPath
'save the uploaded files to the temp directory.
'doing this populates the Upload control's collections
'note that we could also save to memory with the SaveToMemory method
objUpload.Save strPath
'add an Address object to the TOs collection (this specifies the destination address)
objEmail.TOs.Add "[email protected]"
'specify the message subject
objEmail.Subject = "Some Subject"
'specify an SMTP server. Doing this increases the speed and reliability of the mail operation
objEmail.SMTPRelayServers.Add "somesmtpserver.com"
'set the message body
objEmail.Body = "This is the message body"
'now loop through all uploaded files (uploaded via file input boxes), and add
' each uploaded file to the Mail control's Attachments collection
'NOTE: you can use either a For Each loop or a standard For loop here
For Each Item in objUpload.Files
'Note: we are using the OriginalPath property of the UploadedFile object
' (which composes the Files collection) for the ContentName argument of each Attachment
' object. This lets the recipient of the email see the original filename of the attachment,
' (e.g. SomePicture.jpg) instead of the name by which the attachment was saved as. All files
' which are saved to disk have a GUID preceding the original filename used for unique identification.
objEmail.Attachments.Add Item.Path,Item.OriginalPath
Next
'now send the email
objEmail.SendMail
'test for success/failure of the SendMail operation using VBScript's Err object
If Err.Number <> 0 Then
'an error occurred so output the relevant error string
Response.Write "The following error occurred: " & Err.Description
Else
'successful, so output a success message to user
Response.Write "The email was successfully forwarded to the specified SMTP server."
End If
Set objEmail = Nothing 'release resources
Set objUpload = Nothing
%>