1. Introduction
2. Getting Started
3. Attachments
4. Embedded Images
5. Security
6. Unicode
7. Queuing Part I
8. Queuing Part II
Object Reference





Newsletter Signup
File Upload Component
Encryption and Secure Mail Component
Data-bound Editable Grid Component

Chapter 4: Embedded Images Chapter 2: Getting Started Chapter 3. Attachments

The AddAttachment Method

AspEmail enables your application to send email messages with one or many file attachments. To add an attachment to a message, use the method AddAttachment, which expects a full physical path to the file being attached.

The following code sample demonstrates the usage of this method:

<%
' change to address of your own SMTP server
strHost = "mail.elinkisp.com"

If Request("Send") <> "" Then
   Set Mail = Server.CreateObject("Persits.MailSender")
   ' enter valid SMTP host
   Mail.Host = strHost

   Mail.From = "info@persits.com"
   Mail.FromName = "Persits Software, Inc."
   Mail.AddAddress Request("To")

   ' Attach two files in the same directory (any file type can be attached)
   strPath = Server.MapPath(".")
   Mail.AddAttachment strPath & "\ps_logo.gif"
   Mail.AddAttachment strPath & "\wehave.gif"

   ' message subject
   Mail.Subject = "Logo & Motto"
   ' message body
   Mail.Body = "Persits Software logo and motto images are attached."

   Mail.Send ' send message
   Response.Write "Success!"
End If
%>

<HTML>
<BODY BGCOLOR="#FFFFFF">
<FORM ACTION="Attachments.asp">
Enter email: <INPUT TYPE="TEXT" NAME="To">
<INPUT TYPE=SUBMIT NAME="Send" VALUE="Send">
</FORM>
</BODY>
</HTML>

Click the links below to run this code sample:

http://localhost/aspemail/Attachments/Attachments.asp
http://localhost/aspemail/Attachments/Attachments.aspx .NET Version

Memory Attachments (AddAttachmentMem Method)
Starting with AspEmail 5.0, files can be attached not only from disk but also from memory. For example, if your application stores images in the database as blobs, you can attach a database-stored image to a message directly, without first exporting the file to the server's hard drive. This enables you to skip a costly step, and avoid file name collisions.

Memory files are attached via the method AddAttachmentMem which expects two arguments: a filename and a memory blob to copy the file content from. The second argument must be a Variant safe array of bytes.

The following code sample attaches images stored in a MS Access database IMAGES.MDB which is included with the installation. To learn more about storing files in the database as blobs, read this chapter of the Persits Software AspUpload manual.

<%
' change to address of your own SMTP server
strHost = "smtp.broadviewnet.net"

If Request("Send") <> "" Then
   ' Connect to database
   strDbPath = Server.MapPath(".") & "\images.mdb"
   ConnectStr = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & strDbPath

   Set rs = Server.CreateObject("adodb.recordset")
   rs.Open "images", ConnectStr, 2, 3

   If rs.EOF Then
      Response.Write "Recordset is empty."
      Response.End
   End If

   ' Create instance of AspEmail
   Set Mail = Server.CreateObject("Persits.MailSender")
   ' enter valid SMTP host
   Mail.Host = strHost
   Mail.From = "info@persits.com"
   Mail.FromName = "Persits Software, Inc."
   Mail.AddAddress Request("To")

   ' Attach files stored in database as blobs
   While Not rs.EOF
      Mail.AddAttachmentMem rs("name").Value, rs("name").Value
      rs.MoveNext
   Wend

   ' message subject
   Mail.Subject = "Memory Attachments"
   ' message body
   Mail.Body = "Files from the Database are attached."
   Mail.Send ' send message
   Response.Write "Success!"
End If
%>

<HTML>
<BODY BGCOLOR="#FFFFFF">
<FORM ACTION="AttachBlobs.asp">
Enter email: <INPUT TYPE="TEXT" NAME="To">
<INPUT TYPE=SUBMIT NAME="Send" VALUE="Send">
</FORM>
</BODY>
</HTML>

Click the links below to run this code sample:

http://localhost/aspemail/Attachments/AttachBlobs.asp
http://localhost/aspemail/Attachments/AttachBlobs.aspx .NET Version

Uploading Attachments with AspUpload
Code samples above assume that files being attached are already located on the server. However, if a file to be attached is on a client's machine, it must first be uploaded to the Web server, or it cannot be attached.

To upload a file to the server, you may use any upload component. We recommend Persits Software AspUpload®, an evaluation copy of which is included with the AspEmail installation.

The following code sample demonstrates the usage of AspEmail/AspUpload tandem:

<%
   ' change to address of your own SMTP server
   strHost = "mail.elinkisp.com"
   ' We use AspUpload component to capture uploaded file and access other form items.
   ' Because of the special ENCTYPE attribute we can no longer use Request.Form,
   ' we must use Upload.Form instead.
   ' More more info on AspUpload, visit www.aspupload.com.

   Set Upload = Server.CreateObject("Persits.Upload")
   Upload.IgnoreNoPost = True

   ' capture an upload and save uploaded files (if any) in temp directory
   Upload.Save "c:\upload"

   ' We cannot use Upload.Form or Upload.Files until Upload.Save is called.
   If Upload.Form("Send") <> "" Then
      Set Mail = Server.CreateObject("Persits.MailSender")
      Mail.From = "info@persits.com"
      Mail.FromName = "Attachment Demo"
      Mail.Host = strHost
      Mail.Subject = Upload.Form("Subject")
      Mail.Body = Upload.Form("Body")
      Mail.AddAddress Upload.Form("To")

      ' Handle attached file via Upload.Files collection.
      ' Check if a file was ineed uploaded
      If Not Upload.Files("Attachment") Is Nothing Then
         Mail.AddAttachment Upload.Files("Attachment").Path
      End If

      ' We are done. Send message
      Mail.Send
      Response.Write "Success!"
   End If
%>

<HTML>
<BODY BGCOLOR="#FFFFFF">
<!-- Note special ENCTYPE attribute: it is necessary to upload a file-->
<FORM METHOD="POST" ACTION="UploadAttachment.asp" ENCTYPE="multipart/form-data">
<TABLE CELLSPACING=0 CELLPADDING=0>
<TR><TD>Enter email:</TD><TD><INPUT TYPE="TEXT" NAME="To"></TD></TR>
<TR><TD>Enter Subject:</TD><TD><INPUT TYPE="TEXT" NAME="Subject"></TD></TR>
<TR><TD>Enter Body:</TD><TD><TEXTAREA NAME="Body"></TEXTAREA></TD></TR>
<TR><TD>Select File Attachment:</TD><TD><INPUT TYPE=FILE NAME="Attachment"></TD></TR>
<TR><TD COLSPAN=2><INPUT TYPE=SUBMIT NAME="Send" VALUE="Send"></TD></TR>
</TABLE>
</FORM>
</BODY>
</HTML>

Click the links below to run this code sample (ASP and ASP.NET versions, respectively):

http://localhost/aspemail/UploadAttachment/UploadAttachment.asp
http://localhost/aspemail/UploadAttachment/UploadAttachment.aspx .NET Version

Using Memory Uploads with Memory Attachments
AspUpload is capable of uploading files to memory rather than saving them to disk. You can combine this feature with AspEmail's memory attachments to attach files directly from memory for better performance and security.

To use memory uploads, the method Upload.Save should be called without arguments. The binary content of a file is accessed via the property File.Binary.

Instead of

If Not Upload.Files("Attachment") Is Nothing Then
   Mail.AddAttachment Upload.Files("Attachment").Path
End If

the following code should be used:

Upload.Save ' save to memory
....
Set File = Upload.Files("Attachment")
If Not File Is Nothing Then
   Mail.AddAttachmentMem File.Name, File.Binary
End If
...

The code samples UploadAttachmentMem.asp and UploadAttachmentMem.aspx demonstrate this memory uploads/memory attachment technique. Click the links below to run this code sample.

http://localhost/aspemail/UploadAttachment/UploadAttachmentMem.asp
http://localhost/aspemail/UploadAttachment/UploadAttachmentMem.aspx .NET Version

Chapter 4: Embedded Images Chapter 2: Getting Started  


Copyright © 1999 - 2003 Persits Software, Inc.
All Rights Reserved
Questions? Comments? Write us!