Click here to Skip to main content
12,505,591 members (54,552 online)
Click here to Skip to main content
Add your own
alternative version

Stats

17K views
364 downloads
14 bookmarked
Posted

Sending Newsletters from MSMQ Using C# 2.0

, 15 Nov 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Pasan Yasantha
Software Developer
Sri Lanka Sri Lanka
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.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160927.1 | Last Updated 15 Nov 2007
Article Copyright 2007 by Pasan Yasantha
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid