Inbox and Outbox Manipulation in ASP - Inbox using CDO
(Page 4 of 4 )
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:
objSession = Server.CreateObject("CDONTS.Session")
An application using the CDO for NTS Library binds by address to the mailbox you specify in the LogonSMTP parameters:
objSession.LogonSMTP "User Name", "user@example.com"
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")
'this is your logon details
objSession.LogonSMTP "password" , "email@youraddress.com"
'open the inbox folder
Set objInbox = objSession.Inbox
'retrieve the messages collection
Set strMessages = objInbox.Messages
intMsgID = Trim(Request.QueryString("MsgId"))
If ("" = intMsgID Or Not IsNumeric (intMsgID)) Then
'display all the messages
For Each strMessage in strMessages
i = i + 1
Response.Write "<a href="""
Response.Write Request.ServerVariables("SCRIPT_NAME")
Response.Write "?MsgId=" & i & """>" & strMessage.Subject
Response.Write "</a>"
Response.Write " sent by : " & strMessage.Author
Response.Write "<br>"
Next
Else
Set strMessage = strMessages(intMsgID)
Response.Write "Subject : " & strMessage.Subject & "<br>"
Response.Write "Sender : " & strMessage.Sender & "<br>"
Response.Write "Message : " & strMessage.Text
End If
'log off from the session
objSession.LogOff
%>
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.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |