|
This article explains how to send emails from your website using ASP. All you need is to have the IIS with CDONTS components (which is standard with IIS5) installed and the SMTP virtual server running on your web server. Any web hosting service with ASP should already have this already installed on the server.
CDONTS cannot be installed on Win9x systems. On NT4 and Windows 2000, the CDONTS component is installed when you install IIS. Although CDONTS will run on Windows XP, Microsoft has decided to remove the component from IIS 5.1 on Windows XP, so if your server is on Windows XP then you will have to Copy CDONTS.DLL from windows2000 to windows XP(windows/system32) and register it on your IIS web server throught "regsvr32 cdonts.dll" in dos prompt.
To send mails using CDONTS, you need to have the SMTP server that ships with IIS 4 or 5 installed and running on your webserver. The SMTP server is usually installed by default during the standard IIS installation.
Now I shall assume that you have the minimum required knowledge of writing ASP scripts. If you know ASP, then sending mails using CDONTS component is fairly simple and straightforward.
1.Sending Text based email
<%
Dim objMail 'This holds the CDONTS NewMail object
Set objMail = CreateObject("CDONTS.NewMail") 'Creates an instance of the CDONTS.Newmail on the server
objMail.From= "
This e-mail address is being protected from spambots, you need JavaScript enabled to view it
" 'Set the mail ID from which the mail is being sent
objMail.To= "
This e-mail address is being protected from spambots, you need JavaScript enabled to view it
" 'Set the mail ID to which the mail is being sent
objMail.Cc= "
This e-mail address is being protected from spambots, you need JavaScript enabled to view it
;
This e-mail address is being protected from spambots, you need JavaScript enabled to view it
" 'Carbon Copy to be sent if any
objMail.Bcc= "
This e-mail address is being protected from spambots, you need JavaScript enabled to view it
" 'Blind Carbon Copy to be sent if any
objMail.Subject="Type the subject here" 'Set the mail Subject of the mail
objMail.Body= "This will be the message body" 'Set the Body of the message
objMail.Send 'Send the email
Set objMail=nothing 'Free up the server resources
%>
2. Sending HTML based email
By default, CDONTS sends text based emails. If you want to send HTML emails, then all you have to do is add the following 2 extra lines in your code.
objMail.BodyFormat=0
objMail.MailFormat=0
In this case also do not forget to write the body text in the HTML format using HTML tags.For Ex:
objMail.Body=0"<HTML><HEAD><TITLE>ASP HTML Sample</TITLE></HEAD><BODY bgcolor='#000000'><OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' WIDTH='230' HEIGHT='30' id='pl1' ALIGN=''> <PARAM NAME=movie VALUE='pl1.swf'> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#000000> <EMBED src='pl1.swf' quality=high bgcolor=#000000 WIDTH='230' HEIGHT='30' NAME='pl1'></EMBED></OBJECT></BODY></HTML>"
3. Sending attachments with email
To send attachments along with your email, you have to just add the following line in the ASP code.
objMail.AttachFile Server.MapPath("/somefolder/file1.zip")
or if you know the absolute path
objMail.AttachFile("C:\somefolder\file1.zip")
NOTE: You must have the files to be attached to be present on your server (or be accessible on a remote machine by the IUSR_MachineName account) and the IUSR_MachineName must have the Read permission on the File to be attached. To allow any visitor to your page to attach a file to the mail, you'll have to first allow the client machine to upload that file to your server and then attach the file to the mail. But uploading of files can be dangerous and pose a security risk to your server as it would allow users to write files on to your server.
If you try to attach a file that does not exist then you'll receive an Unspecified Error.
NOTE:
-
As it is quite obvious, CDONT scripts can be used to send unauthenticated emails. So be careful while allowing users to send mails using this option.
-
Once a mail is sent using a CDONTS object you cannot use it again to send another mail. For that you'll have to release all its resources by setting it to Nothing and then create a new CDONTS object using CreateObject("CDONTS.NewMail") and then use it send another mail.
本文参考:http://www.hitxp.com, http://www.experts-exchange.com
|