RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Create Your Own Mailing List Server with .NET 2.0  : Page 3

Learn how to build your own mailing list server, making use of the new classes located in the System.Net.Mail namespace in .NET 2.0, as well as some techniques to retrieve emails from Outlook.

Sending Emails
Receiving emails is the hard part; sending emails out to users is extremely easy. All you need to send emails is to use a SmtpClient object and a MailMessage object, both from the System.Net.Mail namespace. Some of the methods in the previous section calls the SendEmail() subroutine, which is defined as shown in Listing 6.

If the Email parameter is empty, the code assumes that you want to send an email to all subscribed users; otherwise, it sends the email only to the email address specified in the Email parameter.

Finally, code the Send Message button as follows:

'---sends a message to all subscribers---
   Private Sub btnSendMessage_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles btnSendMessage.Click

      SendEmail(String.Empty, txtSubject.Text, txtMessage.Text)
      MsgBox("Message sent!")
   End Sub
Testing the Application
Now that you have all the pieces together, let's put it to test. For this application, you have to ensure that Outlook is running before you run your application.

Send an email to the account that you use for the mailing list server with the subject "subscribe." After Outlook finishes downloading new emails, it notifies your application (via the NewMailEx event). The request will be queued and shown in the Queue Status ListBox (see left of Figure 6). About five seconds later, the request in the queue will be processed and your email address will be added to the list of subscribed users (see right of Figure 6).

At the same time, the application will automatically send a confirmation email to you, informing you that you have successfully subscribed to the list.

Figure 6. Subscribe: Subscribing a user to the mailing list.
Figure 7. Sending a Message: Sending a message to all subscribed users.

To unsubscribe from the mailing list, simply send an email with the subject "unsubscribe" to the same email account. As the list administrator, to send an email to all subscribed users simply type in the subject and message and click the Send Message button (see Figure 7).

Basic and Useful
The most critical part of implementing a mailing list server is automating the subscribing and unsubscribing process. Of course, you can enhance it further by detecting bounced emails (such as emails account expiring when people change jobs), etc. Let me know if this application is useful to you.

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date