Click here to Skip to main content
Licence CPOL
First Posted 15 Nov 2007
Views 10,366
Downloads 114
Bookmarked 13 times

Sending Newsletters from MSMQ Using C# 2.0

By | 15 Nov 2007 | Article
This article provides sample code to use MSMQ to send newsletters.

Introduction

Large organizations inform its members about organization events, activities, and other related information through e-newsletters these days. By using Microsoft Visual Studio .NET 2005 and C# 2.0, we can write efficient code to send e-newsletters. In this article, I am going to show how to do that.

Background

In this article, I will use MSMQ (Microsoft Message Queue) for efficiently sending newsletters. For more information regarding MSMQ, please visit http://www.microsoft.com/msj/0798/messagequeue.aspx.

Code

//Required namespaces
using System.Data.SqlClient;
using System.Messaging;
using System.Net.Mail;
using System.Data;

class Newsletter
{
    public Newsletter()
    {
    }

    //This function will send the message to the msmq if the mail server
    //fails to send the email
    private void sendNewsletterMSMQ()
    {
        if (MessageQueue.Exists(@".\private$\Newsletter") == false)
        {
            MessageQueue.Create(@".\private$\Newsletter");
        }

        MessageQueue objQue = new MessageQueue(@".\private$\Newsletter");
        SqlConnection conn = new SqlConnection("server=localhost;uid=sa;" + 
                                 "pwd=password;database=Newsletters;");
        SqlCommand comm = new SqlCommand("SELECT DISTINCT Email " + 
                                         "FROM NewsletterEmails", conn);
        SqlDataReader dr;
        MailAddress from;
        MailAddress to;
        SmtpClient objClient = new SmtpClient();
        objClient.Host = "host";
        objClient.Credentials = new System.Net.NetworkCredential(
                                    "user name", "password");

        try
        {
            conn.Open();
            dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
            while (dr.Read())
            {
                from = new MailAddress("admin@admin.com");
                to = new MailAddress(dr[0].ToString());
                MailMessage objMail = new MailMessage(from, to);
                objMail.Subject = "Newsletter";
                objMail.IsBodyHtml = true;
                objMail.Body = "HTML CODE";

                try
                {
                    objClient.Send(objMail);
                }
                catch
                {
                    objQue.Send(objMail.To[0].Address.ToString());
                }
            }
            dr.Close();
        }
        catch (SqlException sqlEx)
        {
            throw sqlEx;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    //This function will start reading the msmq
    private void readMSMQ()
    {
        MessageQueue objQue = new MessageQueue(@".\private$\Newsletter");
        string[] types = { "System.String" };
        ((XmlMessageFormatter)objQue.Formatter).TargetTypeNames = types;
        objQue.ReceiveCompleted += new ReceiveCompletedEventHandler(queue_ReceiveCompleted);
        objQue.BeginReceive();
    }
 
    //Event handler for the ReceiveCompleted event
    private void queue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
    {
        try
        {
            MessageQueue objQue = new MessageQueue(@".\private$\Newsletter");
            string toAddress = e.Message.Body.ToString();
            sendNewsletterMSMQ(toAddress);
            objQue.BeginReceive();
        }
        catch
        {
        }
    }

    //This function will send the email to the specified address
    private void sendNewsletterMSMQ(string toAddress)
    {
        if (MessageQueue.Exists(@".\private$\Newsletter") == false)
        {
            MessageQueue.Create(@".\private$\Newsletter");
        }

        MessageQueue objQue = new MessageQueue(@".\private$\Newsletter");
        MailAddress from;
        MailAddress to;
        SmtpClient objClient = new SmtpClient();
        objClient.Host = "host";
        objClient.Credentials = new System.Net.NetworkCredential(
                                      "user name", "password");

        try
        {
            from = new MailAddress("admin@admin.com");
            to = new MailAddress(toAddress);
            MailMessage objMail = new MailMessage(from, to);
            objMail.Subject = "Newsletter";
            objMail.IsBodyHtml = true;
            objMail.Body = "HTML CODE";

            try
            {
                objClient.Send(objMail);
            }
            catch
            {
                objQue.Send(objMail.To[0].Address.ToString());
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

Conclusion

By using MSMQ and C# 2.0, we can write code to send newsletters efficiently.

History

  • Version 1.0 - 2007.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pasan Yasantha

Software Developer

Sri Lanka Sri Lanka

Member

Pasan is a Software Engineer at AKLO Information Technologies (Pvt) Ltd, a Colombo based financial services solution provider. He has experience in developing software in c#.net.
 
Pasan holds a Special Honours Degree in Information Technology from Sri Lanka Institute of Information Technology(SLIIT). He has been an MCP since 2006 and currently holds the Microsoft Certified Solution Developer.NET title.


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 15 Nov 2007
Article Copyright 2007 by Pasan Yasantha
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid