The .NET Framework includes a class, System.Web.Mail.SmtpMail that allows you to send mail messages from your application. This class is incredibly easy to use: simply create a message in a System.Web.Mail.MailMessage object, and then pass that message to SmtpMail.Send. As with most things, there are some complicating details if you want to go beyond the most basic kinds of messages. This tip by Jim Mischel from InformIT examines how to diagnose e-mail problems related to your .NET programs.
The SmtpMail class uses the Collaboration Data Objects for Windows 2000 (CDOSYS or CDONTS.DLL) in order to send mail. SmtpMail does not work on Windows 2000 or Windows ME. By default, the class delivers the mail message to a directory on the local computer where the installed SMTP server can pick it up and deliver it. If you want to use a different SMTP server to deliver the mail, you can set the server's name in the SmtpMail.SmtpServer property. A server that sends mail received from your server is called a relay server.
Almost all of the e-mail problems you will encounter will be configuration problems. Specifically, one of the following:
- The SMTP Service on the local computer is configured incorrectly.
- Your Internet firewall is configured to block outgoing SMTP traffic from your computer.
- You did not supply the correct value for the SmtpMail.SmtpServer property.
- The relay server is not configured to accept mail from your computer.
If your program reports an error while sending mail, then you have some information to go on. For example, you might get an error message indicating that you've specified a bad address, that the system couldn't connect with the SMTP server, or that the server refused your message. Those are relatively easy to track down. If you want to use your company's SMTP server, you'll very often need to have the system administrator configure the system to allow message relaying from your computer. If the SMTP server is not configured for relay, your program will not be able to send a message and you will get a runtime error.
Very often, especially if you're using the SMTP service on your local computer to send the messages, your program will report that the message was sent but you never get the message. This happens because your local SMTP service is holding onto the message for some reason. There are several possibilities.
First, it's possible that the SMTP service is not running. The SmtpMail class, through the magic of CDONTS, delivers the mail message that you send directly to the SMTP server's Pickup directory. By default, this is located at C:inetpubmailrootpickup. If there are any files in that directory, it's likely that the SMTP service is not running. You can open the file in Notepad and take a look to see that it's the message you sent. Use the services applet to start the SMTP service.
If there are no messages in the Pickup directory, check the Queue directory. This is where the SMTP service puts messages that it is trying to deliver. They'll sit in the Queue directory for several days while the SMTP services makes repeated attempts to deliver them. There are many reasons why a message might not get delivered on the first attempt, most of which have to do with the receiving SMTP server being unreachable. You can check the System Log in the Event Viewer for messages from the SMTP service (SMTPSVC).
If you find that you can send mail to other people within your domain, but you can't send to outside parties, check with your network administrator to see if outgoing SMTP traffic is being blocked at the firewall.
I've found the default Microsoft SMTP service to be great for delivering mail, provided that you do not try to change any of its default path locations. If you try to move its data storage to anywhere other than "C:inetpubmailroot", it's likely that you will break the service and never get another message to relay through it. If you find yourself in this situation, the easiest and most reliable thing to do is to uninstall the SMTP service, reboot your computer, and then re-install the service. There is a way to accomplish the same thing without uninstalling and re-installing, but it involves a mostly unsupported tool called the MetaBase editor, and a lot of goofing around. Uninstall and re-install is easier and more reliable.
Read more about e-mail in .NET at InformIT.