文章編號: 266452 - 上次校閱: 2006年4月21日 - 版次: 3.3

如何在 ASP 使用 CDONTS 和公佈 Acceptor 傳送附件

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

共同作業資料物件的 Microsoft Windows NT 伺服器 (CDONTS) 傳送電子郵件從動態伺服器網頁 (ASP) 頁面的使用變得很常見。與其他技術一起使用 CDONTS,您可以建立以 Web 為基礎的介面,讓使用者可以傳送他們的電子郵件的附件。在伺服器上的 CDONTS 執行,因此檔案附件必須上載至伺服器之前將它附加至電子郵件。

可以透過 Microsoft 公佈 Acceptor 完成上載檔案。公佈 Acceptor 安裝在伺服器上,並讓用戶端從使用者的電腦傳送至 Web 伺服器的檔案。然後可以到電子郵件當作附件傳送這個檔案。

其他相關資訊

注意:簡易郵件傳送通訊協定 (SMTP) 服務必須啟用,且設定為使用 CDONTS。 (請參閱網際網路資訊服務文件以取得詳細資料)。

將檔案附加到郵件中的步驟

  1. 建立新的 Web 應用程式專案以公佈的 Acceptor 安裝 Web 伺服器上呼叫 myEmailWebApp Microsoft Visual InterDev 中。
  2. 新增稱為 Default.asp 至專案的 ASP 頁面。
  3. 刪除預設隨附頁面程式碼。
  4. 將下列程式碼加入至頁面:
    <%@ Language=VBScript %>
    <HTML>
    <HEAD>
    
    <%
    Dim strServerURL  'Server Name including http:// protocol
    strServerURL = "http://" + Request.ServerVariables("SERVER_NAME")
    
    Dim strServerURLFull 'Path to subweb directory
    strServerURLFull = strServerURL & "/myEmailWebApp"
    
    Dim strTargetURL  'Directory where we will upload our file to
    strTargetURL = strServerURLFull & "/uploaded_files"
    
    Dim strRepostURL  'Path to the page we will display after our file uploads
    strRepostURL = strServerURLFull & "/SendMail.asp"
    
    Dim strPathToPA   'Path to the cpshost.dll
    strPathToPA =  strServerURL & "/Scripts/cpshost.dll"
    
    Dim strPostingURL 'Action path for our form
    strPostingURL = strPathToPA + "?PUBLISH?" + strRepostURL
    %>
    
    
    
    </HEAD>
    <BODY bgcolor="white"> 
    <!--The Form must include the enctype properties set to "multipart/form-data" or the upload will fail. -->
    <FORM enctype="multipart/form-data" action="<%=strPostingURL %>" method="post" id="form1" name="form1">
    <INPUT type="hidden" name="TargetURL" value="<% = strTargetURL %>">
    <!--This hidden field is used to determine if a file post is being made.-->
    <INPUT type="hidden" name="bolAttach" value="true"> 
    	<CENTER>
    		<TABLE bgcolor=Gray border=1>
    			<tr>
    				<td>From:</td>
    				<!--Who is this message from.-->
    				<td><INPUT id="FromBox" name="FromBox" maxlength="50" value="myEmail@myDomain.com"></td>
    			<tr>
    			<tr>
    				<!--Who is this message To.-->
    				<td>To:</td>
    				<td><INPUT id="ToBox" name="ToBox" maxlength="50" value="myEmail@myDomain.com"></td>
    			<tr>
    			<tr>
    				<!--This message's Subject.-->
    				<td>Subject:</td>
    				<td><INPUT id="SubjectBox" name="SubjectBox" maxlength="50" value="Email with attachment.">
    				</td>
    				
    			</tr>
    			<tr>
    				<!--This message's body.-->
    				<td colspan="2"><br><br>Message:<br>
    				<TEXTAREA cols="40" id="MessageArea" name="MessageArea" rows="10">Here is the text for this message.</TEXTAREA>
    				<br>
    				</td>
    			</tr>
    				<tr>
    				<td colspan="2" align="Left" valign="center">
    				Specify File Attachment Paths: 
    				</td>
    			</tr>
    			<tr>
    				<!--Path to the file to attach to this message.-->
    				<td colspan="2" align="right" valign="center">
    				<INPUT name="my_file" type="file" size="20">
    				<br><br>
    				</td>
    			</tr>
    			<tr>
    				<!--This message's importance.-->
    				<td colspan="2" ><br>Importance Level:
    				<INPUT type="radio" id=optImpo name=optImpo value=0 checked>Low
    				<INPUT type="radio" id=optImpo name=optImpo value=1>Medium
    				<INPUT type="radio" id=optImpo name=optImpo value=2>High
    				<br></td>
    			<tr>
    			<tr>
    				<!--Validating Submit Button.-->
    				<td colspan="2" align="right" valign="center">
    				<INPUT type="button" value="Send" id="button1" name="button1" LANGUAGE="javascript" onclick="return button1_onclick()">
    				<INPUT type="reset" value="Reset" id="reset1" name="reset1">
    				</td>
    			</tr>
    		</TABLE>
    	</CENTER>
    </FORM>
    <SCRIPT ID="clientEventHandlersJS" LANGUAGE="javascript">
    <!--
    function button1_onclick() {
    	//Determine if there is a From address at all.
    	if(form1.FromBox.value =="")
    	{
    		//No from Address -Stop Submission
    		alert("You have not typed a From: Address.");
    		return;
    	}
    	//Determine if there is a To address at all.
    	if(form1.ToBox.value =="")
    	{
    		//No To Address -Stop Submission
    		alert("You have not typed a TO: Address.");
    		return;
    	}
    	//Determine if there is a subject at all.
    	if(form1.SubjectBox.value =="")
    	{
    		//No Subject - Question Submission
    		//Determine if they care.
    		var bolCancel = confirm("You have not typed a Subject continue?\n");
    		
    		if(bolCancel==false)
    		{
    			//User clicked Cancel -Stop Submission
    			return;
    		}
    	}
    	if(form1.MessageArea.value =="")
    	{
    		//No Message - Question Submission
    		//Determine if they care.
    		var bolStop = confirm("You have not typed a Message continue?\n");
    		
    		if(bolStop==false)
    		{
    			//User clicked Cancel -Stop Submission
    			return;
    		}
    	}
    	SubmitForm();
    }
    
    function SubmitForm()
    {
       //File counter for the number of files on the Form
       var FileNumber = 0;
       //FormMain is set to the form1 of the document
       var FormMain = document.forms("form1");
       //Loop counter
       var i;
       //Start at zero and loop until i is equal to the number of
       //elements in the page. Step i +1 per loop through
       for (i=0;i<FormMain.elements.length;i++)
       {
    	if (FormMain.elements[i].name == 'my_file' && FormMain.elements[i].value.length > 0)
    	{
    		FileNumber++;
    	}
       }
       //If there are files being posted then send the file to the Posting Acceptor
       //If not, skip to the send mail page with the Attachment boolean set to false.
       if(FileNumber==0)
       {
    	form1.bolAttach.value = "false"
    	form1.action = 'SendMail.asp';
    	//The encoding must be set to a different type in order to not 
    	//use the posting acceptor.
    	form1.encoding = 'application/x-www-form-urlencoded';
    	form1.submit(); 
       }
       else
       {
       //There are files attached use normal submit.
    	form1.submit();
       }
    };
    //-->
    </SCRIPT>
    </BODY>
    </HTML>
  5. 在 Visual InterDev 以滑鼠右鍵按一下專案名稱,在 [專案總管] 中,然後從快顯功能表中選擇 [新增資料夾]。
  6. 命名新的資料夾 Uploaded_files
  7. 開啟網際網路服務管理員主控台,然後瀏覽至您所建立的 Uploaded_files 資料夾。
  8. 在資料夾上按一下滑鼠右鍵,然後按一下 [內容]。
  9. 在 [目錄] 索引標籤上選取 [以 讀/寫權限,然後按一下 [確定]]。
  10. 關閉網際網路服務管理員。
  11. 開啟 [Windows 檔案總管],然後瀏覽至 根目錄] 下的 [MyEmailWebApp] 資料夾: \Inetpub\Wwwroot 目錄。
  12. Uploaded_files] 資料夾上按一下滑鼠右鍵,然後選擇 [內容]。
  13. 在 [安全性] 索引標籤上確定網際網路來賓帳戶在資料夾上擁有 讀取/寫入/變更 權限]。
  14. 按一下 [確定],然後再關閉 [Windows 檔案總管]。
  15. 在 Visual InterDev 加入至 Web 應用程式命名 SendMail.asp 的第二頁。
  16. 刪除預設隨附頁面程式碼。
  17. 將下列程式碼加入至頁面:
    <%@ 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("FromBox")))
    strTo = lcase(Trim(Request.Form("ToBox")))
    strSubject = Request.Form("SubjectBox")
    lngImportance = CInt(Request.Form("optImpo"))
    
    'Create the Message Object
    Set objMsg = Server.CreateObject("CDONTS.NewMail")
    '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("MessageArea")
    objMsg.Importance = lngImportance
    
    'Determine if we need to attach a file.
    Dim bolAttachmentPosted
    bolAttachmentPosted = CBool(Request.Form("bolAttach"))
    If bolAttachmentPosted Then 
    	'If do then where on the server can we get it
    	FilePathOnServer= Server.MapPath("/myEmailWebApp/uploaded_files")
    	FilePath_Name = FilePathOnServer & "\" & Request.Form("FileName") & Request.Form("FileExtention") 
    	'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 ("Scripting.FileSystemObject")
    	'Delete the file from the server
    	FSO.DeleteFile FilePath_Name
    	'Destroy the object
    	Set FSO = Nothing
    End If
    
    'Write out results
    Response.Write "The following message was sent via CDO for NTS:" & "<br>"
    Response.Write "From: " & strFrom  & "<br>"
    Response.Write "To: " & strTo  & "<br>"
    Response.Write "Subject: " & strSubject  & "<br>"
    Response.Write "Importance: " & lngImportance  & "<br>"
    Response.Write "Body: " & Request.Form("MessageArea")  & "<br>"
    
    
    %>
    </BODY>
    </HTML>
    					
  18. 按一下工具列上的 [全部儲存]。
  19. Default.asp] 檔案上按一下滑鼠右鍵,然後選取 [在瀏覽器中的檢視

?考

222618? (http://support.microsoft.com/kb/222618/EN-US/ ) 使用 ASP 和使用者名稱具有公佈 Acceptor
260317? (http://support.microsoft.com/kb/260317/EN-US/ ) 設定要上載至遠端伺服器的 [公佈] Acceptor
189651? (http://support.microsoft.com/kb/189651/EN-US/ ) 上載檔案到 IIS 使用瀏覽器
250384? (http://support.microsoft.com/kb/250384/EN-US/ ) 張貼 Acceptor AllowAnonymous 上載 Windows 上的重新失敗
186204? (http://support.microsoft.com/kb/186204/EN-US/ ) 如何使用 CDONTS 來收集,以及從使用者的郵件資訊
177850? (http://support.microsoft.com/kb/177850/EN-US/ ) 資訊: CDO 1.2 與 CDONTS 差別為何?
189945? (http://support.microsoft.com/kb/189945/EN-US/ ) 如何傳送 HTML 格式郵件透過 SMTP 使用 CDONTS

這篇文章中的資訊適用於:
  • Microsoft Visual InterDev 6.0 Standard Edition?應用於:
    • the operating system: Microsoft Windows 2000
    • Microsoft Windows NT 4.0
關鍵字:?
kbmt kbhowto kbsample kbscript KB266452 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:266452? (http://support.microsoft.com/kb/266452/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。