Home     Products      Support      Corporate     Sign In 
Support Knowledge Base, Article 422
Send Email Attachments with SoftArtisans FileUp and SMTPMail

Using SoftArtisans FileUp and SMTPMail together, it is possible to create a Web-based SMTP client that you can use to compose e-mail messages with attached files.

Using SMTPMail itself to attach files is very easy, as all you need to do is specify the path, on the server, to the file you want to attach with the AddAttachment method. However, what if the file you want to attach isn't yet on the server? In many cases, the file originally resides on the workstation where you'll be sitting while you're composing your email. Or, perhaps you want to create a web application which allows any user to send an e-mail with an attachment. In this case, you'll need to get the file from the client onto the server before it can be attached to an outgoing e-mail. This is where FileUp steps in to the equation.

First, FileUp handles receiving the file you want to attach to your outgoing e-mail. Then, it saves the file temporarily to the server's disk so that SMTPMail can attach it to an outgoing message. Finally, FileUp removes the file from the server after the message has been sent, and you're done.

It is important to note that once FileUp is included in your application, you'll need to make some changes to your form and ASP response page so that the file is transmitted properly, and you can read the form values.
As with any FileUp upload, you'll need to:

  1. Specify ENCTYPE="multipart/form-data" in your <FORM> tag.
  2. To access the values of the form in your ASP script, use FileUp's Form collection instead of ASP's intrinsic Request.Form collection.

The following code illustrates how to utilize both FileUp and SMTPMail together to accomplish this task.

Here is the HTML form that provides the User Interface to our SMTP e-mail application:


<FORM ENCTYPE="multipart/form-data" method="post" action="smtpmail.asp">

    <TD><input type="text" name="remotehost" value="Your SMTP server">
    <TD>From Name:</TD>
    <TD><input type="text" name="fromname">
    <TD>From Address:</TD>
    <TD><input type="text" name="fromaddress">
    <TD><input type="text" name="subject">
    <TD>Recipient's Name:</TD>
    <TD><input type="text" name="recipname">
    <TD>Recipient's E-mail:</TD>
    <TD><input type="text" name="recipemail">
    <TD>Body Text:</TD>
    <TD><textarea rows="15" name="bodytest" cols=50>Enter message body here.</textarea>
    <TD><input type="file" name="attachfile"></TD>
    <TD COLSPAN="2">
    <input type="submit" value="Send Mail w/ Attachment">


Now, here is the ASP response page where FileUp and SMTPMail are used to parse the form data, save the uploaded file, and assemble the outgoing email:

'--- First, we'll use FileUp to receive and temporarily save the uploaded file.
'--- We'll attach it to the e-mail later in the script.
      Set upl = Server.CreateObject("SoftArtisans.FileUp")

      '--- Set the path where you want to save the files
      upl.Path = "c:\temp"

      '--- Here we'll check to be sure the user selected a file to be attached
      If Not upl.Form("attachfile").IsEmpty Then
            '--- If so, set a flag that we'll use later on
            '--- and save the file to the disk
            bAttachFile = True
            '--- If not, set the flag to False
            '--- we'll test this flag later on to prevent errors
            bAttachFile = False
      End If

'--- Now use SMTPMail to attach create an e-mail
'--- Here we take all of the SMTPMail values directly from the form
   Set mailer = Server.CreateObject("SoftArtisans.SMTPMail")
   '--- Set standard SMTPMail properties with values from the Form
      mailer.RemoteHost = upl.Form("remotehost")
      mailer.FromName = upl.Form("fromname")
      mailer.FromAddress = upl.Form("fromaddress")
      mailer.Subject = upl.Form("subject")
      mailer.BodyText = upl.Form("bodytext")
      mailer.AddRecipient upl.Form("recipname"), upl.Form("recipemail")

   '--- Now attach the file that was just uploaded
   '--- .ServerName is the full path and name of the just-saved
      '--- file

      '--- We'll check to be sure there really was an uploaded file
      If bAttachFile Then
            mailer.AddAttachment upl.Form("attachfile").ServerName
      End If

      '--- If the message is sent successfully, delete the file
      '--- from the server
      If mailer.SendMail Then
            Response.Write("Successfully Sent.<BR>")

            If bAttachFile Then
                Response.Write("Attached: " & upl.Form("attachfile").ServerName)
                Response.Write("No attachment.")
            End If
                Response.Write("Mail send failed.")
            End If

            '--- Now, remove the attached file from the server
            '--- since it's already on its way out with the e-mail
            If bAttachFile Then
            End If

Created : 5/30/2003 3:18:47 PM (last modified : 9/2/2004 3:13:47 PM)
Rate this article!