John Alessi has specialized in e-mail development for the past 6 years and has helped many large companies such as Microsoft, Boeing and EarthLink with their e-mail needs. He can be reached at firstname.lastname@example.org.
message onto the Internet
To get your message out onto the Internet, it must be submitted to an SMTP server. The SMTP class of FreeSMTP.Net needs the address of a mail server that will accept your message. In the previous examples we have specified the mail server as mail.yourdomain.com. Of course your mail server will be different, so be sure to change this in your code.
The role of the mail server is to accept your message and see that it gets delivered to the recipient. If the recipient is local (i.e. the same domain as the mail server) the process is all handled internally by the mail server. If the recipient is located outside of your mail server's domain (usually the case), the mail server will find and contact the recipient's mail server and deliver the message to it. This process is called "relaying".
The number one problem that trips up most developers trying to build their first mail enabled application, is that their mail server rejects the messages that are sent to it by their application.
Mostly every mail server on the Internet employs some type of "relay" security. This security prevents spammers from using your mail server as a gateway to get their messages out on the Internet. The side effect of this is that if your mail server does not think that you or your application are authorized to relay mail through it, it will reject your messages. There are several different methods of employing security on mail servers. Some mail servers will only accept mail from certain IP addresses. Some may require your application to authenticate itself by providing a user name and a password before it will allow you to submit mail for non-local delivery. Your mail server administrator should be able to tell you the type of authentication employed.
In any event, if your messages are being rejected by your mail server, you will need to diagnose the problem and take corrective measures so that your server will accept your messages.
The SMTP class of the FreeSMTP.Net assembly contains a LogFile property which is very useful in debugging your applications. Simply set the LogFile property to a file name and it will automatically create a log of the SMTP conversation with the server. By viewing this log in notepad, you can see exactly what the problem is. i.e.:
Here is the output of a log file that I created:
Connect to server. mail.quiksoft.com
220 quiksoft.com MailSite ESMTP Receiver
250-X-IMS 3 10398
250-AUTH SCRAM-MD5 LOGIN CRAM-MD5 NTLM
250 2.0.0 email@example.com OK
501 5.7.1 This system is not configured
to relay mail from
The only item of interest here is the last line which clearly indicates why the server rejected the message. The logging capability or FreeSMTP.Net will save you loads of time diagnosing SMTP problems and is another feature that distinguishes the FreeSMTP.Net classes from System.Web.Mail. System.Web.Mail does not allow you to log the SMTP conversation and therefore is of little help, when you experience problems.
Solving the relay problem
If you are experiencing relaying problems, there are several ways to solve them.
1. Mail server configuration
If your application will be running on the same IP or group of IP addresses every time, your mail server administrator may be able to configure the mail server to accept all mail from those IP addresses.
2. Use a different SMTP server
You may have access to a different server that does not require authentication, or one that has been previously setup to accept messages from you. Another alternative is to install your own dedicated SMTP server which will accept and deliver the messages for you without question. One such server is SMTP Express. There are many other benefits to using a server like SMTP Express. For example, since SMTP Express runs on the same computer as your application, you can submit messages to SMTP Express much faster than submitting them to a remote mail server. In fact the SMTP class of FreeSMTP.Net contains a SubmitToExpress() method that will submit your message to SMTP Express via the file system, which will be about 10x faster than submitting through TCP/IP to a traditional mail server. This means that your application will be more responsive, because SMTP Express will handle the delivery in the background. Additionally, SMTP Express is dedicated to your application which means that your important outbound mail will not be placed at the bottom of the queue of mail already being processed by your main mail server. Among other benefits, SMTP Express will automatically retry failed messages, enable you to monitor the progress of your mail blast, and even send up to 256 messages simultaneously. For more information on SMTP Express, visit http://www.quiksoftcorp.com/smtpexpress.
3. Configure your application to authenticate itself with a user name and password
Please read on to find out how to configure your application to authenticate itself with a user name and password...
Authentication with a user name and password
If your server will allow you to authenticate with a user name and password, you can configure your application to do so. We have been using the FreeSMTP.Net classes thus far, however one feature it does not support is SMTP authentication with a user name and password. For the following example we will use the SMTP class of EasyMail .NET Edition to do the authentication. EasyMail .NET Edition is the big brother to FreeSMTP.Net. It can be downloaded here. The interface is the same as FreeSMTP.Net and is therefore compatible with it. So if you develop your application with FreeSMTP.Net and then want to add more power or features later, upgrading to EasyMail .NET Edition is a snap. Here is the code, using EasyMail .NET Edition which does the authentication:
Quiksoft.EasyMail.SMTP.License.Key = _
"Put your license key here"
Dim msg As New EmailMessage( _
"firstname.lastname@example.org", "Subject...", _
"Message text.", BodyPartFormat.Plain)
Dim smtp As New SMTP()
Dim server As New SMTPServer()
server.Name = "mail.yourdomain.com"
server.AuthMode = SMTPAuthMode.AuthLogin
server.Account = "user"
server.Password = "password"
"Put your license key here";
EmailMessage msg = new EmailMessage(
"Message text.", BodyPartFormat.Plain);
SMTP smtp = new SMTP();
SMTPServer server = new SMTPServer();
The code here is somewhat different to what we have seen in the past. I want to note that the same code we previously wrote for FreeSMTP.Net will compile and run perfectly with the EasyMail .NET Edition classes. But for this example, I wanted to change the code a bit to show you some of the advanced capabilities of EasyMail .NET Edition.
The first difference you will notice is the assignment of a LicenseKey property in the first line of code. EasyMail .NET Edition requires a license key to operate. You can obtain a trial license key or purchase a permanent one from Quiksoft. Another item to note is the creation of the SMTPServer object which is added to the SMTPServers collection. EasyMail .NET Edition enables you to specify an unlimited number of servers for your outgoing message. This is for backup or failsafe operations. Each server in the list is tried until the message is sent successfully. Therefore if your primary mail server is down, the message may still be delivered. In this example however we have only specified one server. The AuthMode, Account and Password properties of the SMTPServer class are used to setup the authentication.
EasyMail .NET Edition supports many more features than FreeSMTP.Net or System.Web.Mail. You can view a comparison here.
That's really it - it's pretty simple. Armed with the knowledge I have supplied and the software such as FreeSMTP.Net or EasyMail .NET Edition you will have your .NET applications sending world class e-mail in no time. In a future edition I will tackle more advanced SMTP topics such as mass mailings, dealing with international character sets, tracking the delivery status of your messages, importing HTML content and more... If you have suggestions for newsletter material involving any other SMTP issue, please let me know.
<< Page 1, Page 2 >>
Copyright 2003, Quiksoft Corporation. Reprinted with permission. Quiksoft offers a free newsletter exposing problems that may be hidden in your applications. Subscribe to Quiksoft's newsletter for critical tips and tricks for e-mail developers.
This was first published in February 2003