Visual Basic Code Example: Sending Messages Using Distribution Lists

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

This example provides a private Sub procedure that sends a message to the destination queues specified in a known distribution list.

For information on distribution list syntax, see Distribution List Format Names.

To send message using a distribution list

  1. Declare the objects needed to send the message. This procedure declares the following objects:

  2. Obtain an MSMQDestination object and initialize it by setting its MSMQDestination.ADsPath property to the distribution list ADs path provided by the caller.

  3. Set message properties. This procedure sets the MSMQMessage.Label property of the message to "Test Message".

  4. Call MSMQMessage.Send to send a copy of the message to each destination queue referenced in the distribution lists.

Note

When using an MSMQDestination object, Message Queuing implicitly opens the destination queues with send access.

Code Example

The following code example requires MSMQ 3.0.

Private Sub SendingToDL( _  
                        strADsPathDL As String _  
                        )  
  
  ' Declare Message Queuing objects.  
  Dim dest As New MSMQDestination  
  Dim msg As New MSMQMessage  
  
  ' Obtain an MSMQDestination object.  
  dest.ADsPath = strADsPathDL  
  
  On Error GoTo ErrorHandler  
  
  ' Set the label of the message.  
  msg.Label = "Test Message"  
  
  ' Send the message to the distribution list.  
  msg.Send DestinationQueue:=dest  
  
  ' Close the MSMQDestination object.  
  dest.close  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Unexpected error!" + Chr(13) +   
         "Reason: " + Err.Description _  
           + Chr(13) + "Error: " + Hex(Err.Number)  
End Sub