Microsoft KB Archive/266452

= How To Send an Attachment in ASP Using CDONTS and the Posting Acceptor =

Article ID: 266452

Article Last Modified on 4/21/2006

-

APPLIES TO

 Microsoft Visual InterDev 6.0 Standard Edition, when used with:  Microsoft Windows 2000 Standard Edition

 Microsoft Windows NT 4.0 

-



This article was previously published under Q266452



SUMMARY
Use of Collaboration Data Objects for Microsoft Windows NT Server (CDONTS) to send e-mail from Active Server Pages (ASP) pages has become very common. By using CDONTS along with other technologies, you can create a Web-based interface that allows users to send attachments with their e-mail. CDONTS executes on the server so the file attachment must be uploaded to the server before it can be attached to the e-mail.

The file upload can be accomplished through the Microsoft Posting Acceptor. The Posting Acceptor is installed on the server and enables a client to post a file to the Web server from the user's computer. This file can then be sent as an attachment to e-mail.



MORE INFORMATION
NOTE: The Simple Mail Transfer Protocol (SMTP) service must be enabled and configured in order to use CDONTS. (See the Internet Information Services documentation for details.)

Steps to Attach a File to a Message
 Create a new Web application project called myEmailWebApp in Microsoft Visual InterDev on the Web server with the Posting Acceptor installed.</li> Add an ASP page called Default.asp to the project.</li> Delete the code that comes with the page by default.</li>  Add the following code to the page: <%@ Language=VBScript %> <HTML> <HEAD>

<% Dim strServerURL 'Server Name including http:// protocol strServerURL = &quot;http://&quot; + Request.ServerVariables(&quot;SERVER_NAME&quot;)

Dim strServerURLFull 'Path to subweb directory strServerURLFull = strServerURL & &quot;/myEmailWebApp&quot;

Dim strTargetURL 'Directory where we will upload our file to strTargetURL = strServerURLFull & &quot;/uploaded_files&quot;

Dim strRepostURL 'Path to the page we will display after our file uploads strRepostURL = strServerURLFull & &quot;/SendMail.asp&quot;

Dim strPathToPA  'Path to the cpshost.dll strPathToPA = strServerURL & &quot;/Scripts/cpshost.dll&quot;

Dim strPostingURL 'Action path for our form strPostingURL = strPathToPA + &quot;?PUBLISH?&quot; + strRepostURL %>

</HEAD> <BODY bgcolor=&quot;white&quot;>

<FORM enctype=&quot;multipart/form-data&quot; action=&quot;<%=strPostingURL %>&quot; method=&quot;post&quot; id=&quot;form1&quot; name=&quot;form1&quot;> <INPUT type=&quot;hidden&quot; name=&quot;TargetURL&quot; value=&quot;<% = strTargetURL %>&quot;>

<INPUT type=&quot;hidden&quot; name=&quot;bolAttach&quot; value=&quot;true&quot;> <CENTER> </CENTER> </FORM> <SCRIPT ID=&quot;clientEventHandlersJS&quot; LANGUAGE=&quot;javascript&quot;>

</SCRIPT> </BODY> </HTML> </li> In Visual InterDev right-click the project name in the Project Explorer and choose New Folder from the pop-up menu.</li> Name the new folder Uploaded_files .</li> Open the Internet Service Manager Console and browse to the Uploaded_files folder that you created.</li> Right-click the folder, and then click Properties.</li> On the Directory tab select read/write permissions, and then click OK.</li> Close the Internet Services Manager.</li> Open Windows Explorer and browse to the MyEmailWebApp folder under the Root:\Inetpub\Wwwroot directory.</li> Right-click the Uploaded_files folder, and then choose Properties.</li> On the Security tab make sure that the Internet guest account has read/write/change permissions on the folder.</li> Click OK, and then close Windows Explorer.</li> In Visual InterDev, add a second page named SendMail.asp to the Web application.</li> Delete the code that comes with the page by default.</li> <li> Add the following code to the page: <%@ Language=VBScript %> <HTML> <HEAD> <TITLE>E-mail Sent Confirmation</TITLE> </HEAD> <BODY> <HR> <% 'Dim Message Variables Dim objMsg, strFrom, strTo, strSubject,lngImportance 'Get the values to use with the message from the post. strFrom = lcase(Trim(Request.Form(&quot;FromBox&quot;))) strTo = lcase(Trim(Request.Form(&quot;ToBox&quot;))) strSubject = Request.Form(&quot;SubjectBox&quot;) lngImportance = CInt(Request.Form(&quot;optImpo&quot;))

'Create the Message Object Set objMsg = Server.CreateObject(&quot;CDONTS.NewMail&quot;) 'Set the properties of the Message objMsg.From = strFrom objMsg.To = strTo objMsg.Subject = strSubject 'Use the actual Request Object so that you do not over load the string Data type. objMsg.Body = Request.Form(&quot;MessageArea&quot;) objMsg.Importance = lngImportance

'Determine if we need to attach a file. Dim bolAttachmentPosted bolAttachmentPosted = CBool(Request.Form(&quot;bolAttach&quot;)) If bolAttachmentPosted Then 'If do then where on the server can we get it   FilePathOnServer= Server.MapPath(&quot;/myEmailWebApp/uploaded_files&quot;) FilePath_Name = FilePathOnServer & &quot;\&quot; & Request.Form(&quot;FileName&quot;) & Request.Form(&quot;FileExtention&quot;) 'Attach it   objMsg.AttachFile FilePath_Name End If

'Send Message objMsg.Send

'Destroy the object Set objMsg = Nothing 'Determine if there is a file on the server to delete. If bolAttachmentPosted Then Dim FSO 'Create a File System Object Set FSO = server.CreateObject (&quot;Scripting.FileSystemObject&quot;) 'Delete the file from the server FSO.DeleteFile FilePath_Name 'Destroy the object Set FSO = Nothing End If

'Write out results Response.Write &quot;The following message was sent via CDO for NTS:&quot; & &quot; &quot; Response.Write &quot;From: &quot; & strFrom & &quot; &quot; Response.Write &quot;To: &quot; & strTo & &quot; &quot; Response.Write &quot;Subject: &quot; & strSubject & &quot; &quot; Response.Write &quot;Importance: &quot; & lngImportance & &quot; &quot; Response.Write &quot;Body: &quot; & Request.Form(&quot;MessageArea&quot;) & &quot; &quot;

%> </BODY> </HTML> </li> <li>Click Save All on the toolbar.</li> <li>Right-click the Default.asp file, and then select View in Browser.</li></ol>

<div class="references_section">