Get the Message - MSMQ

Sending a message

Now we start to get into the details. Before you can use the code that we will show in the rest of this article, you must include a reference to "Microsoft Message Queue Object Library" in your project. We will start by showing you how to send a message. The code shown below illustrates how to post a message to a queue.

Private Sub PostMessage()

Dim QI as New MSMQQueueInfo
Dim RequestQ As MSMQQueue
Dim msg As New MSMQMessage

QI.PathName = "MyServer\QueueName"

Msg.Label = "Sales Order 1"
Msg.Body = "Customer=Bruce;Product=Pokemon;" & _
msg.Send RequestQ


End Sub

First of all, lets' discuss the objects that are used in this code snippet. The MSMQQueueInfo object represents the entire message queue environment. By setting the path name, you are indicating the server on which the queue resides ('MyServer') and the name of the queue ('QueueName'). It is quite possible to have more than one queue on a server. And when you define a message queue, you should try to make the names as meaningful as possible to help with the development process as your use for message queues increases.

By using the Open method, you create an MSMQQueue object that represents the queue specified in PathName. The parameters for Open include the access level for the queue (typically either send or receive) and a locking value that determines if others can have the queue open at the same time (typically, you don't need to open a queue for exclusive access).

The MSMQMessage object represents the message itself. Again, there are a number of properties available, but the most frequently used ones are Label and Body. Label associates a text string with the message. This string gets displayed if you look at the queue details through the MSMQ Explorer. The Body property contains the text that makes up the message. As you will see shortly, this is a simplistic view of the message body, but it will do for now.
Finally, the Send method on the MSMQMessage object is used to post the message to a queue. The MSMQQueue object that is to receive the message is passed as a parameter. At this point, the message has been placed in the specified queue.

You might also like...


About the author

Bruce Johnson Canada

I am the owner of a small application development consulting company that specialized in the design and implementation of Internet-based applications. While there are others who can make a web ...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth