private static void MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult) { MessageQueue myQueue = (MessageQueue)source; //完成指定的异步接收操作 System.Messaging.Message message = myQueue.EndReceive(asyncResult.AsyncResult); MailInfo mi = message.Body as MailInfo; MessageBox.Show(mi.Content); //Thread.Sleep(60000); //myQueue.BeginReceive(); myQueue.BeginReceive(TimeSpan.FromMinutes(0.2)); }
//发送邮件(含事务) private void btnSWSend_Click(object sender, EventArgs e) { MailInfo mi = new MailInfo(); mi.StmpServer = txtSMTP.Text.Trim(); mi.SendAddress = txtFJRDZ.Text.Trim(); mi.SendPwd = txtFJRMM.Text.Trim(); mi.ReceiveAddress = txtSJRDZ.Text.Trim(); mi.Title = txtYJZT.Text.Trim(); mi.Content = txtYJZW.Text.Trim(); if (mi != null) { string queuePath = AppConfig.QueuePathTran; bool isPass = QueueManger.Createqueue(queuePath, true);//创建事务队列 if (isPass == true) { MessageQueueTransaction myTran = new MessageQueueTransaction(); try { myTran.Begin(); isPass = QueueManger.SendMessage <MailInfo>(mi, queuePath, MessagePriority.Highest, myTran);//发送消息到队列 //QueueManger.SendMessage<MailInfo>(mi, queuePath, MessagePriority.Highest, myTran);//发送消息到队列 if (isPass == true) { myTran.Commit(); MessageBox.Show("成功"); } else { myTran.Abort(); MessageBox.Show("失败"); } } catch (Exception ex) { myTran.Abort(); MessageBox.Show("失败原因:" + ex.Message); } } } }
//从消息队列中取出消息并发送 private void btnAllSend_Click(object sender, EventArgs e) { string queuePath = AppConfig.QueuePath; MessageQueue myQueue = new MessageQueue(queuePath); myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MailInfo) }); MessageEnumerator enumerator = myQueue.GetMessageEnumerator2(); while (enumerator.MoveNext()) { MailInfo mi = (MailInfo)enumerator.Current.Body; bool isPass = SendMail(mi); if (isPass == true) { //enumerator.RemoveCurrent(); myQueue.ReceiveById(enumerator.Current.Id); } MessageBox.Show(mi.Content); } }
//从消息队列中取出消息并发送(含事务) private void btnSWAllSend_Click(object sender, EventArgs e) { string queuePath = AppConfig.QueuePathTran; //List<MailInfo> list = QueueManger.GetAllMessage<MailInfo>(queuePath); //foreach(MailInfo mi in list) //{ // MessageBox.Show(mi.Content); // bool isPass = SendMail(mi); // if (isPass == true) // { // //enumerator.RemoveCurrent(); // } // MessageBox.Show(mi.Content); //} //QueueManger.Purgequeue(queuePath); List <MailInfo> list = QueueManger.GetAllMessage <MailInfo>(queuePath); MessageQueueTransaction myTran = new MessageQueueTransaction(); try { myTran.Begin(); for (int i = 0; i < list.Count; i++) { MailInfo mi = QueueManger.ReceiveMessage <MailInfo>(queuePath, myTran); bool isPass = SendMail(mi); MessageBox.Show(mi.Content); } myTran.Commit(); } catch (Exception ex) { myTran.Abort(); MessageBox.Show("出现异常:" + ex.Message); } }