Let me set the scene for you a little. It's about two
hours before I'm supposed to have this week's NewsFlash finished
and sent in and I have no lead article. I check my "things John
is going to write for the site if he ever gets off his butt and
does them" list and nothing on it really works... most of them
will just take too long for me to finish and the few that I
probably could finish probably wouldn't be worth reading with
this little lead time. So here's what I'm going to do...
I'm going to throw you guys a script I was playing with recently
to try and make my own life easier. I'm gonna skip the witty
(or not so witty as the case may be) commentary that usually
goes with these things and just give you the code.
What's This Wonder-Code Supposed to Do?
Good question... once again a little background here will
probably work better then anything else to get the point across.
You know all those email you send using CDO or CDONTS? What
happens to the ones that are addressed wrong? What if the sender
or recipient's address isn't formatted correctly? Well, I'll tell you...
the SMTP service just drops them and leaves them there building up in
its mail directories until you run out of disk space! Well probably
not really... they're pretty small after all, but they do generally
just get left there unless you go clean them out. This normally
is not a big deal, but when something goes wrong like...
Friday I get an email saying that the powers that be think we
need to secure our servers better. They want to shut off incoming
access to all ports except port 80 (for the web server). I'm
all for this... we're not using anything else publicly so I give
the go ahead. Saturday I sleep in and when I finally get my
butt out of bed I check my email, as is my custom. There in my Inbox
are a ton of delay and non-delivery notifications. I was a little
surprised, but I tend to get a lot of them anyway so I just
deleted them and went back to sleep. I get more the
next time I check my mail. I finally get a notice from
someone else using the server who figured out that mail going to
people internal to our private network was working fine, but that
messages to those on the outside were going nowhere.
We're puzzled... after dozens of email back and forth, on Monday
we get them to open back up the ports they locked down in an attempt
to backtrack to a working state. No dice. In the course of the next
day we try every thing we can think of and each time I need to
go see if messages are getting sent or not. To do this I need to:
Fire off the VPN software.
Log into the VPN using 2 certificates and a password.
Curse when it doesn't work on the first try. (Don't ask me why... about 65% of the time the first try fails.)
Log into the VPN again. (Second time always works!)
Fire off my Terminal Server Client.
Type in the server's local IP.
Connect and Log In.
Load Windows Explorer on the remote computer.
Go to our mailroot directory.
Try and remember how many files were in it previously.
See if this number's gone up or down.
If I'm not sure, I sit there and keep hitting refresh to see if they are going up or down.
If they're going down it's working. If they're going up it's not.
Ok so it's not really that bad, but I was getting sick of doing it
so I started playing with some code to let me take a look at these
directories via the web. The resulting code is the code you're about to see.
It's not perfect and it doesn't do all that much, but you could easily
extend it to do lots of stuff. For example, you could set it up so that
you could edit those messages and fix obvious mistakes
and then move the repaired message back to the queue for delivery or add the option to
return the message to the sender, or even just delete it.
As it stands, the code does none of that!
The Code
<%@ Language="VBScript" %>
<% Option Explicit %>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>SMTP Reader</title>
<meta name="author" content="John Peterson" />
</head>
<body bgcolor="#FFFFFF">
<form action="<%= Request.ServerVariables("URL") %>" method="get">
<select name="mbx">
<option>Badmail</option>
<option>Drop</option>
<option>Mailbox</option>
<option>Pickup</option>
<option>Queue</option>
<option>Route</option>
<option>SortTemp</option>
</select>
<input type="submit">
</form>
<%
Const strMailRootPath = "C:\Inetpub\mailroot\"
Dim strMbx
strMbx = Request.QueryString("mbx")
If strMbx <> "" Then
Dim objDropDirectory
Dim colMessageCollection
Dim msgCurrentMessage
Set objDropDirectory = CreateObject("CDO.DropDirectory")
Set colMessageCollection = objDropDirectory.GetMessages(_
strMailRootPath & strMbx)
'Response.Write colMessageCollection.Count
%>
<p>
The <strong><%= strMbx %></strong> folder contains
<strong><%= colMessageCollection.Count %></strong>
messages.
</p>
<table border="1">
<tr>
<th>Filename</th>
<th>From</th>
<th>To</th>
<th>Subject</th>
<th>Sent</th>
<!--<th>Received</th>-->
</tr>
<%
For Each msgCurrentMessage in colMessageCollection
'Set msgCurrentMessage = Server.CreateObject("CDO.Message")
Response.Write "<tr>" & vbCrLf
Response.Write "<td>" & Server.HTMLEncode(_
colMessageCollection.Filename(msgCurrentMessage)) _
& "</td>" & vbCrLf
Response.Write "<td>" & Server.HTMLEncode(_
msgCurrentMessage.From) & "</td>" & vbCrLf
Response.Write "<td>" & Server.HTMLEncode(_
msgCurrentMessage.To) & "</td>" & vbCrLf
Response.Write "<td>" & Server.HTMLEncode(_
msgCurrentMessage.Subject) & "</td>" & vbCrLf
Response.Write "<td>" & Server.HTMLEncode(_
msgCurrentMessage.SentOn) & "</td>" & vbCrLf
' Can throw errors if it hasn't been received anywhere
'Response.Write "<td>" & Server.HTMLEncode(_
' msgCurrentMessage.ReceivedTime) & "</td>" & vbCrLf
Response.Write "</tr>" & vbCrLf
'' If you want to play with a message you can use
'' GetStream to get an ADO.Stream object containing
'' a copy of the message.
'
'Dim objMessageStream 'As ADODB.Stream
'Set objMessageStream = msgCurrentMessage.GetStream
'
'' Get the text to play with:
'Response.Write objMessageStream.ReadText
'
'' Or save the original stream somewhere else:
'objMessageStream.SaveToFile "C:\path\filename.ext"
'
'' Close and kill stream
'objMessageStream.Close
'Set objMessageStream = Nothing
Next
' These could be used to delete a specific message
' or all the messges in a folder.
'colMessageCollection.Delete(2)
'colMessageCollection.DeleteAll
Set colMessageCollection = Nothing
Set objDropDirectory = Nothing
%>
</table>
<%
End If
%>
<br />
<br />
<br />
<p>
Occasionally this script throws an error about the file
being in use because it's being used by another process.
This happens when the SMTP server is processing one of
them. Usually if you wait a minute or two it'll work.
</p>
</body>
</html>