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
using System.Data.SqlClient;
using System.Messaging;
using System.Net.Mail;
using System.Data;
class Newsletter
{
public Newsletter()
{
}
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;
}
}
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();
}
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
{
}
}
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
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.