Search Persits Software Knowledge Base

 

Article PS01072965

HOWTO: Using a remote file as a message body and/or attachment

Problem Description

AspEmail methods such as AddAttachment or AppendBodyFromFile only accept physical paths as arguments, but not URLs. This article describes how to use the XUpload component in conjunction with AspEmail to retrieve remote files by their URLs and attach them to email messages.

Solution

To use a remote file with AspEmail, it must first be downloaded to the server's hard drive where AspEmail can have access to it. Another component is required to download a file programmatically from a URL. We recommend using XUpload available from www.aspupload.com although any other HTTP component can be used as well.

The following code sample downloads a GIF image from the URL http://www.persits.com/ps_logo.gif and attaches it to an email message via the method AddAttachment .

To run this code sample, you must download XUpload.exe from here and register the file xupload.ocx on the server using regsvr32 . To retrieve a remote file from a URL, we use XUpload's method MakeHTTPRequest . Here, we use three out of its 5 parameters: HTTP method (we use GET), HTTP parameters (we use none), and a local path.

<%
Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = "smtp.mycompany.com" ' use your own SMTP host
Mail.From = "me@mycompany.com" ' Sender's address

Mail.AddAddress "him@hiscompany.com"

Mail.Body = "Here it is attached."
Mail.Subject = "Persits Software Logo"

' Obtain logo from Persits.com. Use XUpload (www.aspupload.com)
Set XUpload = Server.CreateObject("Persits.XUpload")

' download file to current directory
LocalPath = Server.MapPath("persits.gif")

' specify URL of image
XUpload.Server = "www.persits.com"
XUpload.Script = "/ps_logo.gif"

' download file to server's hard drive
XUpload.MakeHttpRequest "GET", "", LocalPath

' Attach newly downloaded file
Mail.AddAttachment LocalPath

Mail.Send

' it is usually a good idea to delete the temp file
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFile LocalPath
%>

The following code sample retrieves a remote HTML file generated by an ASP script, and uses it for the message body. Here we use the MakeHTTPRequest method in a slightly different manner: instead of specifying a local destination path for the file being downloaded, we use the method's return value to obtain the file as a string which we then assign to the Mail.Body property. Notice that the Path parameter is omitted which instructs the method to return the remote file as a string rather than saving it to disk.

<%
Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = "smtp.mycompany.com" ' use your own SMTP host
Mail.From = "me@mycompany.com" ' Sender's address

Mail.AddAddress "him@hiscompany.com"

Mail.Subject = "AspEncrypt Hash Demo"

' Obtain remote file from Support.Persits.com. Use XUpload (www.aspupload.com)
Set XUpload = Server.CreateObject("Persits.XUpload")

' specify URL of file (as ASP script in this case)
XUpload.Server = "support.persits.com"
XUpload.Script = "/encrypt/demo_hash.asp"

' Copy remote file content to a variable
Body = XUpload.MakeHttpRequest("GET", "", , "", "")

Mail.IsHTML = True
Mail.Body = Body

Mail.Send
%>

Created: 2/8/2002 4:27:44 AM
Last Modified: 2/9/2002 4:10:31 PM

Copyright © Persits Software, Inc. 1998 - 2018
For technical support, write to support@persits.com.