Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / Windows Forms

Use of MSMQ for Sending Bulk Mails

Rate me:
Please Sign up or sign in to vote.
4.76/5 (15 votes)
29 Mar 2011CPOL3 min read 66.2K   6.2K   52  
Use of MSMQ for Sending bulk mails
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Messaging;


namespace EmailSender
{
    /// <summary>
    /// 
    /// </summary>
    public class EmailService
    {
        /// <summary>
        /// Queues the message.
        /// </summary>
        /// <param name="emailMessage">The email message.</param>
        public void QueueMessage(EmailEntities.EmailMessage emailMessage)
        {
            try
            {
                if (emailMessage == null)
                {
                    return;
                }
                string msmqQueuePath = @".\Private$\EmailQueue";
                Message msmqMsg = new Message();
                msmqMsg.Body = emailMessage;
                msmqMsg.Recoverable = true;
                msmqMsg.Formatter = new BinaryMessageFormatter();
                MessageQueue msmqQueue = new MessageQueue();
                //If the Message queue does not exists at specified location create it
                if (!MessageQueue.Exists(msmqQueuePath))
                {
                   msmqQueue =  MessageQueue.Create(msmqQueuePath);
                }                
                msmqQueue.Formatter = new BinaryMessageFormatter();
                msmqQueue.Send(msmqMsg);

            }
            catch(Exception oEx)
            {
                throw oEx;
            }
          

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) Atos Origin
India India
Over 6+ years of experience in Software Design Development,Worked on C#, ADO.NET, ASP.NET, Rational XDE, UML, MS SQL Server - 2000,2005,2008, .Net FrameWork - 1.1,2.0,3.5, Crystal Reports.

Education - B.E - E&TC, CDAC - DAC.

Certifications:

Microsoft: MCTS - in .Net FrameWork 2.0.
IBM: Certified solution designer.
BrainBench: Certified in C#,Asp.Net,Framework 3.5,Asp.Net3.0.

Comments and Discussions