Sending e-mail with .NET, the easy way

Learn the basics of sending e-mail with .NET, includes sample code.

This Content Component encountered an error
This Content Component encountered an error

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 john@quiksoftcorp.com.


You can send robust e-mail messages from your .NET app within minutes
This tip was taken from an issue of the E-mail Secrets newsletter. You will learn the basics of sending e-mail with .NET and we will take a look at the issues pertaining to the needs of most developers and explain them in an easy to understand way. We will try to stay away from complicated topics such as MIME, Base64, etc. Most of the software that we will use to send the messages is free and shields the developer from a learning curve. Most importantly, we will show you how to start sending e-mail from your .NET apps right away. The included sample code can be downloaded and you can be sending e-mail like a pro in minutes. Read on...

In this edition you will learn:

  • The basics of sending e-mail
  • How to attach files to your messages
  • How to send an HTML message that contains support for non HTML mail readers
  • Several reasons why the System.Web.Mail classes are not your best bet
  • How to send e-mail through a server that requires authentication
  • How to overcome the #1 problem most developers encounter when trying to send e-mail from their application

This edition also contains sample code that will enable you to:

  • Create and send an e-mail message with only one line of code
  • Create and send a message containing an attachment
  • Create and send an HTML e-mail message with text support for non HTML readers
  • Send a message through a server that requires authentication
  • Specify backup servers for your outgoing message

Do you really want to look under the hood?
Sending e-mail is either incredibly complicated, or a very simple task. It all depends on your perspective. Every day we get into our cars and drive from here to there, mostly not worrying about the mechanics under the hood. E-mail is similar. Users of popular e-mail software can send an e-mail message without having to worry about the low level details such as RFC-822, MIME, Base64, etc.

Developers of e-mail applications often find themselves wrestling with these details, but that does not have to be so. With the advent of several excellent developer tools (assemblies in .NET speak), any developer with even trivial .NET experience can send e-mail like the pros without worrying about the complexities "under the hood."

I am not going to dwell on these complexities, because I don't believe that you need to understand them to build a successful e-mail enabled application. If you appreciate the finer details of the process, there are plenty of RFC's on the Internet which you can spend countless hours deciphering. Suffice it to say that I have done that so you don't have to. For those who want to build dependable applications that send e-mail, without investing in a large learning curve, read on.

The very basics
Even though I promised not to waste your time and/or bore you with the low level details, I want to give you a basic primer on e-mail technology because I think that it will help you ground yourself and enable you to relate what I have to say in this article with other things you may read.

E-mail is delivered across the Internet with the SMTP protocol. The SMTP protocol dictates how e-mail messages are submitted to the Internet and delivered to the recipient. The construct of each message is governed by many other specifications. The specifications for SMTP and the construct of messages are contained in the multitude of RFC's floating around the Internet.

Without diving into all the internals, I want you to know that the code in this newsletter will enable you to build messages that take advantage of all the latest e-mail technologies while preserving compatibility across the different mail servers and readers on the net. Enough said, lets get on with it.

The following sample will show you how to send e-mail from your .NET applications with only one line of code.

The absolutely easiest and fastest way to send email
This one line of code will create and send an e-mail message. It uses the FreeSMTP.Net assembly, which can be downloaded here. More on FreeSMTP.Net in a minute.

VB Sample
Quiksoft.FreeSMTP.SMTP.QuickSend( _
     "mail.yourdomain.com", _
     "recipient@domain.com", _
     "sender@domain.com", _
     "Subject...", _
     "Message text.", _
     Quiksoft.FreeSMTP.BodyPartFormat.Plain)

C# Sample
Quiksoft.FreeSMTP.SMTP.QuickSend(
     "mail.yourdomain.com",
     "recipient@domain.com",
     "sender@domain.com",
     "Subject...",
     "Message text.",
     Quiksoft.FreeSMTP.BodyPartFormat.Plain);

It doesn't get much easier than this. Add a reference to the FreeSMTP.Net assembly, and call the QuickSend method. Because the QuickSend() method is static, you do not even have to instantiate a class. The method will even allow the sending of HTML messages, by setting the last parameter to BodyPartFormat.HTML.

FreeSMTP.Net is free as its name implies. But don't think that because it is free, that it is not good. FreeSMTP.Net is produced by Quiksoft who also produces the popular EasyMail Objects, EasyMail Advanced API and EasyMail .NET Edition. Quiksoft specializes in e-mail technologies for professional developers. FreeSMTP.Net is offered by Quiksoft as an introduction to its product line. It is not often that you find quality in things for free, but FreeSMTP.Net is clearly an exception.

Sending an attachment
Since the QuickSend() method does not support attachments, we will instantiate the FreeSMTP.Net EmailMessage object, add the attachments and send it with an instance of the FreeSMTP.Net SMTP class. As you can see, all this requires only a few lines of code.

VB Sample Dim msg As New EmailMessage( _
     "recipient@domain.com", _
     "sender@domain.com", "Subject...", _
     "Message text.", _
     BodyPartFormat.Plain)
msg.Attachments.Add("c:attachment.txt")
Dim smtp As New SMTP("mail.yourdomain.com")
smtp.Send(msg)

C# Sample
EmailMessage msg = new EmailMessage(
     "recipient@domain.com",
     "sender@domain.com", "Subject...",
     "Message text.",
     BodyPartFormat.Plain);
msg.Attachments.Add("c:attachment.txt");
SMTP smtp = new SMTP("mail.yourdomain.com");
smtp.Send(msg);

Sending HTML message with support for non HTML mail readers
To send an HTML message with support for non HTML mail readers, we must actually send two message bodies. One is formatted in HTML and the other in plain text. Mostly every non HTML mail reader will be able to ignore the HTML and display the text body instead.

Many people are using the System.Web.Mail class to send mail through their .NET apps, but there are many reasons why this might not be the best route. We will touch on this again, but for now I would like to point out that the System.Web.Mail class does not support sending more than one body format in the same message. Therefore, the System.Web.Mail classes do not allow you to send an HTML message with support for non HTML readers.

VB Sample
Dim msg As New EmailMessage( _
     "recipient@domain.com", _
     "sender@domain.com", "Subject...", _
     "Text message.", BodyPartFormat.Plain)
Dim html As New String( _
     "HTML message.")
msg.BodyParts.Add(html, BodyPartFormat.HTML)
Dim smtp As New SMTP("mail.yourdomain.com")
smtp.LogFile = "c:smtp log.txt"
smtp.Send(msg)

C# Sample
EmailMessage msg = new EmailMessage(
     "recipient@domain.com",
     "sender@domain.com", "Subject...",
     "Text message.", BodyPartFormat.Plain);
string html =
     "HTML message.";
msg.BodyParts.Add(html, BodyPartFormat.HTML);
SMTP smtp = new SMTP("mail.yourdomain.com");
smtp.LogFile="c:smtp log.txt";
smtp.Send(msg);

In this example we instantiate an EmailMessage class using the overloaded constructor to create a plain text message. Next we add a new body part to the message formatted as HTML. The FreeSMTP.Net assembly will automatically use this information to create a message that contains both plain text and HTML versions of the message. Beneath the hood, the Send() method builds a MultiPart Alternative MIME message. The developer is shielded from the complexity of MIME, but can rest assured that their message is compatible with mostly every mail reader.

Continue on to find out about the #1 problem that affects most developers sending e-mail from their apps...

<< Page 1, Page 2 >>

This was first published in February 2003

Dig deeper on Microsoft .NET Web services

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close