How to: Send Email from a Microsoft Server Application

Friday, June 18, 2010 / Posted by Luke Puplett /

If you have a server application, web site or even a job or task running on a Windows server, you may need to send out emails. For me, it was an ASP.NET MVC 2 application; I needed to send an email to people who stuck their address in the interested parties register on the coming soon page on to confirm it was a genuine request. But you could have a build server that you want to email out a report from or some other thing. This guide is more about the server side setup than the SMTP client you send, although I will give examples in .NET and DOS, via Blat.exe.

To make things easy, I’ll bullet the whole process.

  • First, ensure that you cannot use an existing Exchange Server or MTA in your organisation. It makes life so much easier when someone else deals with these things because, as the person coding or scripting, you should only care about getting the job done.

  • After you’ve failed to convince the jobsworth in IT security, install your own one. In Windows Server 2008 (Web Edition, also) open Server Manager > Features node > Right-click > Add Feature > locate SMTP Server and install the requisite IIS 6 components as well as Telnet Client.

  • If you are using a hosted or public internet facing server, you should now ensure that your server had a PTR Record for its IP address. The people that gave you your RIPE internet address can do this (usually your host or ISP). Do this now so they can get started.

  • To prevent spam, most MTAs will do a reverse DNS (rDNS) lookup on your server to ensure you have the same domain name as you claim to be sending email from. The style of address your server has can also trigger a spam detector, so avoid a reverse DNS address such as as it looks like the dynamic ranges given out to broadband home users (who are blacklisted on SMTP servers by default).

  • Check and make sure that your IP address is not blacklisted before you begin.

  • Set the Simple Mail Transport Protocol service to start automatically.

  • You may want to add an smtp CNAME into your DNS server so you can move it in future without editing code or config files.

  • In IIS 7 Manager, locate your site and then the SMTP E-mail icon and set the email address and check the radio button to deliver to localhost on port 25, or the DNS alias you setup. Two things to note: setting delivery to a pick-up directory is useful on development boxes when you don’t want to flood the mail servers with real email. The .eml files can be opened in Outlook. Also, the other settings on this page may actually be ignored by clients/your app, but I’m setting them in any case.

  • Open IIS 6 Manager and you should just have the SMTP node in there.

  • Right-click the SMTP virtual server and go to Properties.

  • Tab 1: Tick Enable logging and set a valid path.

  • Tab 2: Under Authentication, set Anonymous access only for the time being. Under Connection, check All except the list below and under Relay, check All except the list below and tick Allow all computers at the bottom.

  • Tab 3: Check the Badmail directory you can fill in the Send copy… box but it may not work anyway, at first. Leave the other settings.

  • Tab 4: Set all timeouts and delays to minimum. This is so the server stops trying and gives you an error to look at quickly. You should reset these after setting up/troubleshooting. Enter Outbound Security and make sure that its set to Anonymous.

  • You should review the other tabs but otherwise leave them for now and click OK.

  • Now rename the domain to just the domain part and close IIS 6 Manager.

  • Open a DOS prompt and test that your SMTP server is listening. telnet localhost 25 or whatever your server name is. Note “Microsoft ESMTP MAIL Service, Version: 7.5”: 7.5 eh? What’s all this needing IIS 6 all about then?

  • Download Blat.exe and ‘install’ a default profile like so:

    blat -install -f -server localhost -port 25 -try 1 -profile myprofile
  • Now you can try sending a test message like so:

    blat - -to -f -p myprofile -body "This is a test." -subject "This is a test message."
  • Hotmail has a very clearly written Non-delivery Report (NDR) message, which is why I choose them, also you’ll hear the ping from Live Messenger if it works.

  • Remember to check your Junk folder!

  • Open Event Viewer and ensure nothing was logged by the SMTP service. I use a catch all filter for “Everything in the last hour”.

  • Open the c:\inetpub\mailroot folder and keep an eye on what’s in each of the folders. Personally, I’ve found that Badmail doesn’t seem to gather anything and that the Drop folder has the most interesting information. EML files can be opened in Notepad and the Drop folder is like the Inbox; it will receive NDRs that contain error messages.

  • Also, check the SMTP service log file. A successful connection should be visible in the log as OutboundConnectionResponse lines. If these are not there, then it is having a problem connecting to the remote mail server.

  • Check that the remote MTA is up. Use to find the MX record and thus the mail server for the recipient. Check it’s up.

  • Ensure that port 25 is open in your firewall.

  • Attempt to telnet to the remote server.

  • Give up and use

When you get Blat.exe sending successfully, then you can use the SmtpClient class to very easily fire out your emails from your ASP.NET app.

Good luck!

Labels: , , ,


Post a Comment