There are numerous ways to send and receive emails using ASP. The simplest way is to use CDO. CDO is a free emailing component which comes packaged with NT Option Pack 4. If you want to receive email, you can use the CDO session object. This article will explain ways to sending and receive email through ASP 3.0. After you finish reading it, you will be able to design a simple Web page for sending and receiving email.
Sending Emails Using CDO: Outbox
Sending email through Active Server Pages is not a difficult task. There are too many ASP components that are used to send email using ASP. One method growing in popularity is the Collaborative Data Object (CDO). One reason for its popularity is that it is a fast and free method of sending email. Another reason is that it ships with NT Option Pack 4 by default, so you don’t need to buy and install it.
To start sending email, the first step is to create an instance of a CDO object in your ASP code. It is as simple as:
<%
dim objMail
set objMail = Server.CreateObject("CDONTS.NewMail")
%>
CDONTS.NewMail has easy to use properties and methods. These include ".TO," ".From," ".CC," ".BCC," ".Subject," and ".Body." Let's look at some code:
<%
objMail.To = "burhansk@hotmail.com"
objMail.From = "user@yahoo.com"
objMail.cc = "me@mydomain.com"
objMail.bcc = "me_bcc@mydomain.com"
objMail.Subject = "This is subject line"
objMail.Body = "Here is body of email"
objMail.Send
%>
In the above code, I initialize CDO properties, and use .Send to send email. One wonderful thing about CDO is that, if you use an incorrect email address in properties, CDO will never send through any error, but it also will not send email. So correct email addresses are compulsory.
CDO also has too many features, like file attachment, priority, HTML body, and so forth. I am not going to explain these features of CDO because too many articles have already been written on this issues. Let's explore some other issues.
CDO is not everything, it is just one email component. There are many others faster and more powerful then CDO. The one major limitation of CDO is that it can only be used on Windows NT Server and does not provide certain useful options for sending email. This limitation decreases its use in other major platforms. Actually, CDO is only designed to send quick text-based email. To get extra features we need to use some other component.
Other than CDO, I personally use the Emia4Win (http://www.system-engine.com/index.shtml) component. Emia4Win is free to use, and its primary purpose is to make it easier and faster for a developer to create a full-fledged webmail application. Emia4Win is a bare-bones command line driven IMAP/POP3/SMTP package for sending and retrieving electronic mail. It can be used as a utility/component with the intent to give Web/Internet applications email capabilities. It can be incorporated into any application regardless of what language is used.
So no matter what component you use, each provides the ability to send email. However some are faster and some slower. Enewsletterpro.com performed some speed tests on a Celeron 500 MHZ machine running Windows 2000 and the Windows SMTP mail server. Their statistics are shown below:
One of the most commonly asked questions on any ASP related forum is “How can I receive email thought CDONTS?” The simple answer is “You can use CDONTS.Session to receive email in your machine.” The SMTP (Simple Mail Transfer Protocol) Service handles standard SMTP messages, which are just text files that are transmitted between hosts using the SMTP. So, in theory, we can create, modify and read SMTP mail messages just as we would any other text file.
Inbox in Machine:
The first thing you need to do is to make sure that you have installed Microsoft's SMTP service, which you will find as part of the Windows NT 4 Option Pack. When you have completed the Option Pack installation you will find that, within your inetpub directory (along with WINNT directory), there will be a subdirectory called mailroot. Within this mailroot directory are various other subdirectories used for processing incoming and outgoing SMTP email. Pickup and Queue are used for the sending process, and Badmail is where anything that gets bounced by the remote server will end up, while Drop is where any incoming emails will appear.
The next thing that you need to do is to announce to the great wide world that this server in now accepting SMTP emails. For this purpose you have to associate it with a domain name. You can do this by adding records within your DNS system. One record is a MX record (MX means Mail Exchange). MX record tells other servers where to send your incoming SMTP emails. The sequence will be:
First, MX keyword, which tells that it is Mail Exchange record.
Second, Priority, it can be any integer number. Priority is useful in this sense that if your server went down, and main while emails come to your server, emails will be lost. So you can set some other servers and assign priorities to each. If height priority server is down, then server of next lower priority will be used for backup emails.
Finally, Server, which will accept SMTP mails. Like mail.mydomain.com
For example: if your domain name is mydomain.com, the MX record will be:
MX 10 mail.mydomain.com
MX 20 mail.mydomain2.com
The next thing you need to do is set up the domain name in Default SMTP sites. To do this, open IIS, in the Default SMTP sits, Right-Click on Domains, and select New -> Domain. Then choose Local, and type your domain name. That is all that is required to set up your IIS configurations.
Now you can try to see whether it is working. For this purpose send an email to your domain, say me@mydomain.com. Now as I already said, all emails will come into the Drop directory of mailroot directory. So check the Drop directory; you will find new email with the extension EML. The file name will be any random number + .eml extension. You can double click on that file and see your received email. This email will be open in your default mail express. You can open it in Notepad and see the original message with all headers and source code.
We can also read messages by opening them as text files with the FileSystemObject. This may be useful for specific tasks where we are building applications that use email messages in the background to transfer information or send commands, without the messages being visible to the user in the normal way. By opening email file in this way you will also see some headers included in email. Such as:
Received: from 202.147.176.60 by by1fd.bay1.hotmail.msn.com with HTTP;
Thu, 22 Jan 2005 11:31:33 GMT
X-Originating-IP: [202.147.176.60]
X-Originating-Email: [abc@hotmail.com]
X-Sender: abc@hotmail.com
From: "My Name” abc@hotmail.com
You can code to filter these headers and show the original message to the user. And you can use any application for this purpose such as VB, C++ etc…
You can also see these email messages by using ASP. A Session object is considered a top-level object, meaning it can be created directly from a MS VB program. The Session object contains session-wide settings and options. Microsoft's CDO (Collaborative Data Object) allows you to process this incoming email as a series of objects. First you need to create an instance of CDONTS.Session. It can be created like this:
When CDO for NTS is running with IIS, the Inbox is a common folder shared by all recipients and applications, and containing all undeleted messages received by IIS. After these two steps, you can use properties ( ".inbox," ".outbox," ".version," etc…) of CDONTS.Session object to get your desired results.
' open the inbox folder
Set objInbox = objSession.Inbox
' retrieve the messages collection
Set strMessages = objInbox.Messages
' Go to specific message
Set strMessage = strMessages(intMsgID)
' retrieve message information
strMessage.Subject ' subject of message
strMessage.Sender ' sender
strMessage.Text ' actual message
After all, you need to log off your CDONTS session by using the ".LogOff" method.
objSession.LogOff
That is all the work you need to do to receive email from ASP code.
To review, the whole code needed to check the inbox in ASP is:
<%
'create our variables
Dim objSession , strMessage , strMessages , objInbox , intMsgID
'create an instance of the CDONTS.Session object
Set objSession = Server.CreateObject("CDONTS.Session")
In this code MsgId is a query string value which shows that you want to see any specific email message. If MsgId is not given in the URL, then all of the emails’ subjects will be shown, so you can select any email.
Conclusion
Sending and receiving email from Web pages is obviously useful because you often cannot use Outlook Express or other mail programs such as Eudora and Amoel to receive emails. This may be because you are far from your computer, in another city or country, or enjoying your trip or away with family and friends for the holidays. Using a Web page for sending and receiving email can help you access your email throughout the world.