Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

I was working with a co-worker the other day trying to see what our emails looked like when they were sent from a webpage.  We had some formatting issues and wanted to see the actual message on the server right after it was generated.  The problem, we couldn't capture it fast enough on the server.

I realized one small trick, turn just the SMTP service off that comes with IIS. Messages queue up in the local pickup folder.   After you turn the service on, the messages are delivered as normal.    This works both for troubleshooting webpages generated with classic asp, asp.net or a 3rd party script.  Here is an example of what I did.

An update.  I double checked this and ran into some workarounds in regards to .NET testing.

A few assumptions

1) This will fail if you have your SMTP Serivce not configured to accept email locally.   You'll get a 'refused message'.
2) The code needs to run on the same server
3) For .NET 1.1 and 2.0 websites, you are using the System.Web.Mail (1.1 namespace) code sample.  I could not get a webpage with System.Net.Mail namespace to work. 

Stop the service.

Generate a message

Look in the pickup folder

Poof! The message is still there.  Your webpage will still work.  I get a lot of questions in the newsgroups asking where are my messages or what do my messages look like.  You can review the email headers in Notepad while it remains in the pickup folder.  If you have several messages in the folder, just use the Search feature to look inside files for your message.

Here are the headers from the sample email.


X-Receiver: steve@example.com
X-Sender: steve@example.com
Thread-Topic: Sending email with CDO
thread-index: Acd4LFre6hIxY5VuRXC44UTFQUbrpQ==
From: <steve@example.com>
To: <steve@example.com>
Subject: Sending email with CDO
Date: Fri, 6 Apr 2007 05:17:10 -0400
Message-ID: <000001c7782c$5af81390$3e00a8c0@aspdot.net>
MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028

This is a message.

Here is the ASP code used to generate the message.

<%
Set myMail=Server.CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From=steve@example.com
myMail.To=steve@example.com
myMail.TextBody="This is a message."
myMail.Send
set myMail=nothing

response.write "Email was sent at " & Now()
%>

Hope that helps in your SMTP troubleshooting.

Published Friday, April 06, 2007 5:20 AM by steve schofield
Filed under: ,

Comments

# re: Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

Sunday, April 08, 2007 6:53 PM by Keith Reid

That's odd.  I would have expected that by turning off the service you'd never get the message delivered at all.  I'd expect SMTP port to be unresponsive.

# re: Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

Sunday, April 08, 2007 9:37 PM by steve schofield

It would be if you were trying to deliver email to the box.  It does not affect the creation of emails by asp and asp.net on the machine itself.  

# re: Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

Monday, April 09, 2007 3:47 PM by Ryan Smith

Interesting, that doesn't seem to work for me at all.

I get an error from both ASP.NET and PHP when I try to send email to an SMTP server that is stopped.  When it tries to send the mail, it gets a "Server refused connection" and doesn't know where to send the mail.

Maybe I'm doing something differently though.

# re: Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

Monday, April 09, 2007 5:30 PM by steve schofield

It has to be on the 'same' server.

# re: Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

Monday, April 16, 2007 10:13 AM by Anthony

I'm getting the "System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed." error message and have totaly failed to Google an answer. I tries your suggestion but get the same result. Any suggestions most welcome!

# re: Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

Monday, April 16, 2007 8:58 PM by steve schofield

Recall this needs to be a local connection and the code can not be using the system.net.mail namespace (.NET 2.0).  Also make sure the '127.0.0.1' is configured to accept email.

# re: Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

Thursday, April 19, 2007 2:24 AM by Cam

It also depends on how the mail object is configured in the ASP code.

If you're using a drop folder, the ASP page will simply write the email into that folder where it will sit until the service restarts.

If it is configured to connect to the SMTP port, it will not work...it will get an error.

# re: Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

Wednesday, April 25, 2007 10:32 AM by Piyush

I have been using this trick on my 1.1 web app for a while. Works great for the test server where you do not want your e-mails going out but still have them somewhere to look at. But this no longer works with 2.0 as you mention. I have been hunting for the solution but this blog entry was the only one I could find that even acknowledges the problem let alone offer a solution. Do you have any thoughts on this inconsistency between 1.1 and 2.0? Any pointers?

# re: Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

Saturday, May 05, 2007 3:38 AM by steve schofield

Hi Piyush

I'm not sure why asp.net 2.0 acts different.  The person who runs www.systemnetmail.com might know the answer or a workaround.  I've not tried this trick with ASPNetEmail (Dave Wanta), creator of this fine product might know. Otherwise, the only other people would be people on the ASP.NET team.  

# re: Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

Thursday, May 24, 2007 4:31 PM by Marilyn

I've got the same issue with asp.net 2.0. I'm also interested if anyone has a workaround.

# re: Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

Tuesday, June 26, 2007 6:38 PM by Piyush

I figured out the solution to this problem. Apparently it's not the limitation of 2.0 but it's the additional flexibility that is the problem. The new SmtpClient class now provides a property called DeliveryMethod and it has the following choices: Network, PickupDirectoryFromIis and SpecifiedPickupDirectory. By default it's the "Network", which means it tries to send the e-mail by itself. That obviously fails if the SMTP service is stopped. So if you want to avoid that then choose, yes you guessed it, "PickupDirectoryFromIis". This can be done in Web.Config or in code-behind.

<system.net>

<mailSettings>

<smtp deliveryMethod="PickupDirectoryFromIis" > </smtp>

</mailSettings>

</system.net>

</configuration>

Some links of interest:

www.dotnet2themax.com/.../default,date,2006-04-22.aspx

msdn2.microsoft.com/.../system.net.mail.smtpdeliverymethod(vs.80).aspx

# re: Want to troubleshoot your email created on Windows using the SMTP Service? Turn the Service off

Wednesday, August 08, 2007 12:40 AM by Arvind Waghmare

ya it works thanks