65.9K
CodeProject is changing. Read more.
Home

Sending Newsletters from MSMQ Using C# 2.0

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.81/5 (8 votes)

Nov 15, 2007

CPOL
viewsIcon

26764

downloadIcon

411

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.