Monitor MS SMTP queue from ASP.Net
I came across a situation today where I needed to monitor the number of files in the Microsoft SMTP queue from a web page. While there are numerous ways I considered approaching this, I wanted it to be simple, portable, and easy to maintain.
I decided to fire up Visual Studio 2010 and come up with a quick solution. While I usually choose to use VB.Net, this was a good opportunity to practice up on a little C#.
After creating a new C# project, I added a new page, smtpqueue.aspx. In keeping with simplicity I created it without a code-behind page opting to put the C# code in a <script> tag in the header of the page.
Getting the file was easy using the Directory class from System.IO. Getting the count of files was even easier. I then went back and added a threshold and did some basic math to generate a friendly color-coded status on the page.
Here is the full code of the page that you can easily copy and paste into notepad and save as an aspx file. You will note that this code is set to monitor the default C:\inetpub\mailroot\queue folder, you can easily modify it to monitor any folder. You may also have to adjust the NTFS permissions for this to work correctly.
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<head id=”Head1″ runat=”server”>
<title>SMTP Queue Count</title>
<script language=”c#” runat=”server”>
public void Page_Load(object sender, EventArgs e)
const int threshold = 100;
string dir = @”C:\inetpub\mailroot\queue\”;
files = System.IO.Directory.GetFiles(dir);
numFiles = files.Length;
ltlThreshold.Text = threshold.ToString();
if (numFiles < threshold)
ltlStatus.Text = “<span style=’color:green’>Good</span>”;
ltlStatus.Text = “<span style=’color:red’>Error</span>”;
<form id=”form1″ runat=”server”>
Queue file count: <asp:Literal runat=”server” ID=”ltlCount”></asp:Literal><br />
Threshold: <asp:Literal runat=”server” ID=”ltlThreshold”></asp:Literal><br />
Status: <asp:Literal runat=”server” ID=”ltlStatus”></asp:Literal>
Rick is a Senior Support Lead at OrcsWeb, a hosted server company providing managed hosting solutions.