ID Artikel: 266452 - Kajian Terakhir: 22 September 2011 - Revisi: 2.0

Bagaimana cara mengirim lampiran di ASP dengan menggunakan CDONTS dan menerima Posting

Tips SistemThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

Pada Halaman ini

Perbesar semua | Perkecil semua

RINGKASAN

Penggunaan benda-benda Data kolaborasi untuk Server Windows NT Microsoft (CDONTS) untuk mengirim e-mail dari halaman Active Server Pages (ASP) telah menjadi sangat umum. Dengan menggunakan CDONTS bersama dengan teknologi lainnya, Anda dapat membuat antarmuka berbasis Web yang memungkinkan pengguna untuk mengirim lampiran dengan e-mail mereka. CDONTS mengeksekusi pada server sehingga lampiran file harus upload ke server sebelum dapat dilampirkan ke e-mail.

File upload dapat dicapai melalui Microsoft Posting akseptor. Kotoran akseptor Posting diinstal pada server dan memungkinkan klien untuk mengirimkan file ke Web server dari pengguna komputer. File ini kemudian akan dikirim sebagai lampiran ke e-mail.

INFORMASI LEBIH LANJUT

CATATAN: Layanan protokol Transfer surat sederhana (SMTP) harus diaktifkan dan dikonfigurasi untuk menggunakan CDONTS. (Lihat dokumentasi Internet Information Services untuk rincian).

Langkah-langkah untuk melampirkan File ke pesan

  1. Membuat proyek aplikasi Web baru yang disebut myEmailWebApp di Microsoft Visual InterDev pada Web server dengan Posting akseptor diinstal.
  2. Tambahkan halaman ASP yang disebut Default.asp untuk proyek.
  3. Menghapus kode yang datang dengan halaman secara default.
  4. Tambahkan kode berikut ke halaman:
    <%@ 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. Dalam Visual InterDev klik kanan nama proyek di Explorer proyek dan pilih Folder baru dari pop-up menu.
  6. Nama folder baru Uploaded_files.
  7. Buka Internet Service Manager konsol dan browse ke Uploaded_files folder yang Anda buat.
  8. Klik kanan folder, dan kemudian klik Properti.
  9. Pada Direktori Pilih tab baca/tulis izin, lalu klik Oke.
  10. Tutup Internet Services Manager.
  11. Buka Windows Explorer dan browse ke folder MyEmailWebApp di bawah Root: \Inetpub\Wwwroot direktori.
  12. Klik kanan Uploaded_files folder, dan kemudian memilih Properti.
  13. Pada Keamanan tab pastikan bahwa account Tamu Internet telah membaca/menulis/perubahan izin pada folder.
  14. Klik Oke, kemudian tutup Windows Explorer.
  15. Dalam Visual InterDev, menambahkan halaman kedua bernama SendMail.asp untuk aplikasi Web.
  16. Menghapus kode yang datang dengan halaman secara default.
  17. Tambahkan kode berikut ke halaman:
    <%@ 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. Klik Simpan semua di toolbar.
  19. Klik kanan Default.asp file, dan kemudian pilih Lihat dalam Browser.

REFERENSI

222618  (http://support.microsoft.com/kb/222618/EN-US/ ) Menggunakan ASP dan nama pengguna dengan Posting akseptor
260317  (http://support.microsoft.com/kb/260317/EN-US/ ) Mengkonfigurasi penerima Posting untuk meng-Upload ke server jauh
189651  (http://support.microsoft.com/kb/189651/EN-US/ ) Meng-upload File ke IIS menggunakan Browser
250384  (http://support.microsoft.com/kb/250384/EN-US/ ) Posting akseptor AllowAnonymous meng-Upload Repost kegagalan pada Windows
186204  (http://support.microsoft.com/kb/186204/EN-US/ ) Cara menggunakan CDONTS untuk mengumpulkan dan E-mail informasi dari pengguna
177850  (http://support.microsoft.com/kb/177850/EN-US/ ) INFO: Apakah perbedaan antara CDO 1,2 dan CDONTS?
189945  (http://support.microsoft.com/kb/189945/EN-US/ ) Bagaimana cara mengirim HTML diformat Mail ke SMTP menggunakan CDONTS

Berlaku bagi:
  • Microsoft Visual InterDev 6.0 Standard Edition
Kata kunci: 
kbhowto kbsample kbscript kbmt KB266452 KbMtid
Penerjemahan MesinPenerjemahan Mesin
PENTING: Artikel ini diterjemahkan menggunakan perangkat lunak mesin penerjemah Microsoft dan bukan oleh seorang penerjemah. Microsoft menawarkan artikel yang diterjemahkan oleh seorang penerjemah maupun artikel yang diterjemahkan menggunakan mesin sehingga Anda akan memiliki akses ke seluruh artikel baru yang diterbitkan di Pangkalan Pengetahuan (Knowledge Base) dalam bahasa yang Anda gunakan. Namun, artikel yang diterjemahkan menggunakan mesin tidak selalu sempurna. Artikel tersebut mungkin memiliki kesalahan kosa kata, sintaksis, atau tata bahasa, hampir sama seperti orang asing yang berbicara dalam bahasa Anda. Microsoft tidak bertanggung jawab terhadap akurasi, kesalahan atau kerusakan yang disebabkan karena kesalahan penerjemahan konten atau penggunaannya oleh para pelanggan. Microsoft juga sering memperbarui perangkat lunak mesin penerjemah.
Klik disini untuk melihat versi Inggris dari artikel ini:266452  (http://support.microsoft.com/kb/266452/en-us/ )