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

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

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

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

标签:
原文地址:http://www.cnblogs.com/tonykan/p/4416077.html