 System.Messaging.Message message = new System.Messaging.Message();
System.Messaging.Message message = new System.Messaging.Message(); message.Priority = MessagePriority.Highest;  //最高消息优先级
message.Priority = MessagePriority.Highest;  //最高消息优先级
 private void btnSend_Click(object sender, EventArgs e)
private void btnSend_Click(object sender, EventArgs e) {
{ //连接到本地的专用队列myQueue
    //连接到本地的专用队列myQueue MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
    MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue"); System.Messaging.Message message = new System.Messaging.Message();
    System.Messaging.Message message = new System.Messaging.Message(); message.Label = tbName.Text;
    message.Label = tbName.Text; message.Body = tbContext.Text;
    message.Body = tbContext.Text;
 if (cbPriority.Checked)
    if (cbPriority.Checked) {
    { message.Priority = MessagePriority.Highest;
        message.Priority = MessagePriority.Highest; }
    } else
    else {
    { message.Priority = MessagePriority.Normal;
        message.Priority = MessagePriority.Normal; }
    } myQueue.Send(message);
    myQueue.Send(message); MessageBox.Show("成功发送消息到队列");
    MessageBox.Show("成功发送消息到队列"); }
}

 private void DisplayMessage()
private void DisplayMessage() {
        { //连接到本地队列
            //连接到本地队列 MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
            MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue"); myQueue.MessageReadPropertyFilter.Priority = true;
            myQueue.MessageReadPropertyFilter.Priority = true; DataTable messageTable = new DataTable();
            DataTable messageTable = new DataTable(); messageTable.Columns.Add("名字");
            messageTable.Columns.Add("名字"); messageTable.Columns.Add("消息内容");
            messageTable.Columns.Add("消息内容"); messageTable.Columns.Add("优先级");
            messageTable.Columns.Add("优先级"); XmlMessageFormatter formatter = new XmlMessageFormatter(new string[] { "System.String" });
            XmlMessageFormatter formatter = new XmlMessageFormatter(new string[] { "System.String" }); try
            try {
            { //从队列中接收消息
                //从队列中接收消息 System.Messaging.Message[] messages = myQueue.GetAllMessages();
                System.Messaging.Message[] messages = myQueue.GetAllMessages(); for (int index = 0; index < messages.Length; index++)
                for (int index = 0; index < messages.Length; index++) {
                { messages[index].Formatter = formatter;
                    messages[index].Formatter = formatter;
 string label = messages[index].Label;
                    string label = messages[index].Label; string body = messages[index].Body.ToString();
                    string body = messages[index].Body.ToString(); string priority = messages[index].Priority.ToString();
                    string priority = messages[index].Priority.ToString();
 messageTable.Rows.Add(new string[] { label, body, priority });
                    messageTable.Rows.Add(new string[] { label, body, priority }); }
                } this.dgvMessage.DataSource = messageTable;
                this.dgvMessage.DataSource = messageTable; }
            } catch (MessageQueueException e1)
            catch (MessageQueueException e1) {
            { MessageBox.Show(e1.Message);
                MessageBox.Show(e1.Message); }
            } }
        } }
    }
 private void btnRec_Click(object sender, EventArgs e)
private void btnRec_Click(object sender, EventArgs e) {
{ DisplayMessage();
    DisplayMessage(); }
}
 // 摘要:
// 摘要: //     指定消息队列在消息传递到队列的过程中应用于该消息的优先级,以及指定何时将消息插入目标队列。
//     指定消息队列在消息传递到队列的过程中应用于该消息的优先级,以及指定何时将消息插入目标队列。 public enum MessagePriority
public enum MessagePriority {
{ // 摘要:
    // 摘要: //     最低消息优先级。
    //     最低消息优先级。 Lowest = 0,
    Lowest = 0, //
    // // 摘要:
    // 摘要: //     位于 Low 和 Lowest 消息优先级之间。
    //     位于 Low 和 Lowest 消息优先级之间。 VeryLow = 1,
    VeryLow = 1, //
    // // 摘要:
    // 摘要: //     低消息优先级。
    //     低消息优先级。 Low = 2,
    Low = 2, //
    // // 摘要:
    // 摘要: //     普通消息优先级。
    //     普通消息优先级。 Normal = 3,
    Normal = 3, //
    // // 摘要:
    // 摘要: //     位于 System.Messaging.MessagePriority.High 和 System.Messaging.MessagePriority.Normal
    //     位于 System.Messaging.MessagePriority.High 和 System.Messaging.MessagePriority.Normal //     消息优先级之间。
    //     消息优先级之间。 AboveNormal = 4,
    AboveNormal = 4, //
    // // 摘要:
    // 摘要: //     高消息优先级。
    //     高消息优先级。 High = 5,
    High = 5, //
    // // 摘要:
    // 摘要: //     位于 Highest 和 High 消息优先级之间。
    //     位于 Highest 和 High 消息优先级之间。 VeryHigh = 6,
    VeryHigh = 6, //
    // // 摘要:
    // 摘要: //     最高消息优先级。
    //     最高消息优先级。 Highest = 7,
    Highest = 7, }
}

 //创建普通的专用消息队列
//创建普通的专用消息队列 MessageQueue myMessage = MessageQueue.Create(@".\private$\myQueue");
MessageQueue myMessage = MessageQueue.Create(@".\private$\myQueue"); //创建事务性的专用消息队列
//创建事务性的专用消息队列 MessageQueue myTranMessage =MessageQueue.Create(@".\private$\myQueueTrans", true);
MessageQueue myTranMessage =MessageQueue.Create(@".\private$\myQueueTrans", true); //连接到本地的队列
//连接到本地的队列 MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue"); Message myMessage = new Message();
Message myMessage = new Message(); myMessage.Body = "消息内容";
myMessage.Body = "消息内容"; myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); //发送消息到队列中
//发送消息到队列中 myQueue.Send(myMessage);
myQueue.Send(myMessage);
 //连接到本地的队列
//连接到本地的队列 MessageQueue myQueue = new MessageQueue(".\\private$\\myQueueTrans");
MessageQueue myQueue = new MessageQueue(".\\private$\\myQueueTrans");
 Message myMessage = new Message();
Message myMessage = new Message(); myMessage.Body = "消息内容";
myMessage.Body = "消息内容"; myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
 MessageQueueTransaction myTransaction = new MessageQueueTransaction();
MessageQueueTransaction myTransaction = new MessageQueueTransaction(); //启动事务
//启动事务 myTransaction.Begin();
myTransaction.Begin(); //发送消息到队列中
//发送消息到队列中 myQueue.Send(myMessage, myTransaction);  //加了事务
myQueue.Send(myMessage, myTransaction);  //加了事务 //提交事务
//提交事务 myTransaction.Commit();
myTransaction.Commit(); Console.WriteLine("消息发送成功!");
Console.WriteLine("消息发送成功!");
 //连接到本地队列
//连接到本地队列 MessageQueue myQueue = new MessageQueue(".\\private$\\myQueueTrans");
MessageQueue myQueue = new MessageQueue(".\\private$\\myQueueTrans"); myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); if (myQueue.Transactional)
if (myQueue.Transactional) {
{ MessageQueueTransaction myTransaction = new MessageQueueTransaction();
    MessageQueueTransaction myTransaction = new MessageQueueTransaction(); //启动事务
    //启动事务 myTransaction.Begin();
    myTransaction.Begin(); //从队列中接收消息
    //从队列中接收消息 Message myMessage = myQueue.Receive(myTransaction);
    Message myMessage = myQueue.Receive(myTransaction); string context = myMessage.Body as string; //获取消息的内容
    string context = myMessage.Body as string; //获取消息的内容 myTransaction.Commit();
    myTransaction.Commit(); Console.WriteLine("消息内容为:" + context);
    Console.WriteLine("消息内容为:" + context); }
}
 /// <summary>
/// <summary> /// 发送消息到队列
/// 发送消息到队列 /// </summary>
/// </summary> private static void SendMessage()
private static void SendMessage() {
{ MessageQueue myQueue = new MessageQueue(".\\private$\\myAsyncQueue");
    MessageQueue myQueue = new MessageQueue(".\\private$\\myAsyncQueue"); if (myQueue.Transactional)
    if (myQueue.Transactional) {
    { Book book = new Book();
        Book book = new Book(); book.BookId = 1001;
        book.BookId = 1001; book.BookName = "ASP.NET";
        book.BookName = "ASP.NET"; book.BookAuthor = "ZhangSan";
        book.BookAuthor = "ZhangSan"; book.BookPrice = 88.88;
        book.BookPrice = 88.88; Message message = new Message(book);
        Message message = new Message(book); message.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.Async.Book) });
        message.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.Async.Book) });
 MessageQueueTransaction myTransaction = new MessageQueueTransaction();
        MessageQueueTransaction myTransaction = new MessageQueueTransaction(); myTransaction.Begin();
        myTransaction.Begin(); myQueue.Send(message, myTransaction);
        myQueue.Send(message, myTransaction); myTransaction.Commit();
        myTransaction.Commit(); Console.WriteLine("消息成功发送到队列!");
        Console.WriteLine("消息成功发送到队列!"); }
    } }
}
 /// <summary>
/// <summary> /// 异步接收消息
/// 异步接收消息 /// </summary>
/// </summary> private static void AsyncReceiveMessage()
private static void AsyncReceiveMessage() {
{ MessageQueue myQueue = new MessageQueue(".\\private$\\myAsyncQueue");
    MessageQueue myQueue = new MessageQueue(".\\private$\\myAsyncQueue"); if (myQueue.Transactional)
    if (myQueue.Transactional) {
    { MessageQueueTransaction myTransaction = new MessageQueueTransaction();
        MessageQueueTransaction myTransaction = new MessageQueueTransaction(); //这里使用了委托,当接收消息完成的时候就执行MyReceiveCompleted方法
        //这里使用了委托,当接收消息完成的时候就执行MyReceiveCompleted方法 myQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted);
        myQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted); myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.Async.Book) });
        myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.Async.Book) }); myTransaction.Begin();
        myTransaction.Begin(); myQueue.BeginReceive();//启动一个没有超时时限的异步操作
        myQueue.BeginReceive();//启动一个没有超时时限的异步操作 signal.WaitOne();
        signal.WaitOne(); myTransaction.Commit();
        myTransaction.Commit(); }
    } }
}
 private static void MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult)
private static void MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult) {
{ try
    try {
    { MessageQueue myQueue = (MessageQueue)source;
        MessageQueue myQueue = (MessageQueue)source; //完成指定的异步接收操作
        //完成指定的异步接收操作 Message message = myQueue.EndReceive(asyncResult.AsyncResult);
        Message message = myQueue.EndReceive(asyncResult.AsyncResult); signal.Set();
        signal.Set(); Book book = message.Body as Book;
        Book book = message.Body as Book; Console.WriteLine("图书编号:{0}--图书名称:{1}--图书作者:{2}--图书定价:{3}",
        Console.WriteLine("图书编号:{0}--图书名称:{1}--图书作者:{2}--图书定价:{3}", book.BookId.ToString(),
            book.BookId.ToString(), book.BookName,
            book.BookName, book.BookAuthor,
            book.BookAuthor, book.BookPrice.ToString());
            book.BookPrice.ToString()); myQueue.BeginReceive();
        myQueue.BeginReceive(); }
    } catch (MessageQueueException me)
    catch (MessageQueueException me) {
    { Console.WriteLine("异步接收出错,原因:" + me.Message);
        Console.WriteLine("异步接收出错,原因:" + me.Message);
 }
    } }
}

 protected void btnSendMail_Click(object sender, EventArgs e)
protected void btnSendMail_Click(object sender, EventArgs e) {
{ //取出数据存入MailInfo对象
    //取出数据存入MailInfo对象 MailInfo info = new MailInfo();
    MailInfo info = new MailInfo(); info.Title = tbTitle.Text;
    info.Title = tbTitle.Text; info.Content = tbContent.Text;
    info.Content = tbContent.Text; info.StmpServer = tbSmtpServer.Text;
    info.StmpServer = tbSmtpServer.Text; info.Sender = tbSender.Text;
    info.Sender = tbSender.Text; info.SenderPwd = tbSenderPwd.Text;
    info.SenderPwd = tbSenderPwd.Text; info.ReceiveAddress = tbReceive.Text;
    info.ReceiveAddress = tbReceive.Text;
 if (info != null)
    if (info != null) {
    { CreateQueue();
        CreateQueue(); SendMessage(info);
        SendMessage(info); }
    } }
}
 封装了邮件的属性
封装了邮件的属性 /// <summary>
/// <summary> /// 连接消息队列并从队列中接收消息
/// 连接消息队列并从队列中接收消息 /// </summary>
/// </summary> private MailInfo ReceiveMessage()
private MailInfo ReceiveMessage() {
{ MailInfo info = null;
    MailInfo info = null; //连接到本地队列
    //连接到本地队列 MessageQueue myQueue = new MessageQueue(".\\private$\\myMailQueue");
    MessageQueue myQueue = new MessageQueue(".\\private$\\myMailQueue"); myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MailInfo) });
    myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MailInfo) }); try
    try {
    { if (myQueue.Transactional)
        if (myQueue.Transactional) {
        { MessageQueueTransaction myTransaction = new MessageQueueTransaction();
            MessageQueueTransaction myTransaction = new MessageQueueTransaction(); //启动事务
            //启动事务 myTransaction.Begin();
            myTransaction.Begin(); //从队列中接收消息
            //从队列中接收消息 Message myMessage = myQueue.Receive(myTransaction);
            Message myMessage = myQueue.Receive(myTransaction); info = myMessage.Body as MailInfo; //获取消息的内容
            info = myMessage.Body as MailInfo; //获取消息的内容 myTransaction.Commit();
            myTransaction.Commit(); return info;
            return info; }
        }
 }
    } catch (MessageQueueException e)
    catch (MessageQueueException e) {
    { this.tdError.InnerText = e.Message;
        this.tdError.InnerText = e.Message; }
    } return info;
    return info; }
}
 protected void Button2_Click(object sender, EventArgs e)
protected void Button2_Click(object sender, EventArgs e) {
{ if (info != null)
    if (info != null) {
    { SmtpClient client = new SmtpClient();
        SmtpClient client = new SmtpClient(); client.Host = info.StmpServer;
        client.Host = info.StmpServer; client.UseDefaultCredentials = false;
        client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(info.Sender, info.SenderPwd);
        client.Credentials = new NetworkCredential(info.Sender, info.SenderPwd); client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
 MailMessage message = new MailMessage(info.Sender, info.ReceiveAddress);
        MailMessage message = new MailMessage(info.Sender, info.ReceiveAddress); message.Subject = info.Title;
        message.Subject = info.Title; message.Body = info.Content;
        message.Body = info.Content; message.BodyEncoding = Encoding.UTF8;
        message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true;
        message.IsBodyHtml = true;
 try
        try {
        { //发送邮件到目标地址
            //发送邮件到目标地址 client.Send(message);
            client.Send(message); this.tdSucces.InnerText = "邮件已成功发送到目标地址:" + info.ReceiveAddress;
            this.tdSucces.InnerText = "邮件已成功发送到目标地址:" + info.ReceiveAddress; }
        } catch (Exception ex)
        catch (Exception ex) {
        { this.tdError.InnerText = "发送失败,失败原因:" + ex.Message;
            this.tdError.InnerText = "发送失败,失败原因:" + ex.Message; }
        } }
    } }
}

原文:http://www.cnblogs.com/tonykan/p/4416077.html