Recipe7.14.Deleting Messages from a Queue


Recipe 7.14. Deleting Messages from a Queue

Problem

You need to delete messages from a queue on your Exchange virtual server.

Solution

Using a graphical user interface

  1. Open the Exchange System Manager (Exchange System Manager.msc).

  2. Expand the organization Administrative Groups target administrative group Servers.

  3. The right pane will display all queues on this server, including MTA, SMTP, and foreign connector queues.

  4. Click the Source field of the header to group the queues according to the component that they are associated with and find the appropriate SMTP virtual server.

  5. Click on a queue to select it and click Find Messages, or double-click the queue to open the search window. By default, the search will return 100 messages at a time.

  6. If you wish to restrict the search, modify the search parameters.

  7. Click Find Now.

  8. Select the message to delete by clicking on it; you can use Shift and Ctrl with additional clicks to select multiple messages. Right-click your selection, select one of the two Delete options, and confirm the deletion.

Using VBScript
' This code deletes a given message within an Exchange SMTP queue. ' ------ SCRIPT CONFIGURATION ------ ' The host name of the Exchange server strHostname = "<ExchangeServerName>" ' e.g., red-exch02 ' Name of the SMTP queue to search for messages strQueueName = "<SMTPQueueName>" ' e.g., 3sharp.com ' Message ID of the message to delete strMessageID = "<MessageID>" ' ------ END CONFIGURATION --------- ' Get the Exchange Namespace WMI object Set objWMIExch =  GetObject("winmgmts://" & strHostname &_    "/root/MicrosoftExchangeV2")  ' Get the list of queues and process our desired queue Set objQueuesList = objWMIExch.InstancesOf("Exchange_SMTPQueue") For Each objQueueInst in objQueuesList    ' Make sure this queue is the one we're looking for; if not, skip it    If objQueueInst.QueueName = strQueueName Then       strMsgInfo = strMsgInfo & "Queue: " & objQueueInst.QueueName & " (" &_          objQueueInst.QueueID & ")" & VbCrLF       Set objMsgsList = objWMIExch.ExecQuery ("Select * From " &_        "Exchange_QueuedSMTPMessage Where ProtocolName='SMTP' And LinkId='" &_          objQueueInst.LinkID & "' And LinkName='" &_          objQueueInst.LinkName & "' And QueueId='" &_          objQueueInst.QueueID & "' And QueueName='" &_          objQueueInst.QueueName &"' And VirtualMachine='" &_          objQueueInst.VirtualMachine & "' And VirtualServerName='" &_          objQueueInst.VirtualServerName & "' And MessageID ='" &_          strMessageID & "'")        For each objMsgInst in objMsgsList          strMsgInfo = strMsgInfo & "  Message " & _             objMsgInst.MessageID & VbCrLF &_             "    Sender: " & objMsgInst.Sender & VbCrLF &_             "    MessageID: " & objMsgInst.MessageId & VbCrLF          objMsgInst.DeleteNoNDR          strMsgInfo = strMsgInfo & "  Message deleted." & VBCrLF       Next    End If Next Wscript.Echo strMsgInfo

Discussion

This recipe is an extension of Recipe 7.13. When you use ESM to delete messages, you go into the Queue Viewer, select your queue, search for your messages, select one or more of them, and select one of the two deletion options from the right-click context menu: Delete (with NDR) and Delete (no NDR). As their names imply, one will generate an NDR, the other won't.

This same simplicity exists in the Exchange Server 2003 WMI classes. While the Exchange_QueuedMessage class provides all of the properties needed for both SMTP and X.400 messages, the Exchange_QueuedSMTPMessage and Exchange_QueuedX400Message classes extend those properties with four methods, shown in Table 7-6.

Table 7-6. Exchange Server 2003 WMI message handling methods

Method

Description

Freeze

This method freezes the message in the queue; Exchange will not attempt to deliver it.

Thaw

This method tells Exchange to resume delivery attempts for this message.

DeleteNDR

This method deletes the given message and generates an NDR to the original sender.

DeleteNoNDR

This method deletes the given message without generating an NDR.


The script uses essentially the same code and methodology as Recipe Recipe 7.13. The only difference is that we add the specific MessageID property to the WQL query so that it returns only the specific message we wish to delete. We then use the DeleteNoNDR method to remove the message from the queue.

Both etiquette and codified RFC best practices (particularly section 6.1 of RFC 2821) say you should always generate an NDR when deleting a message, especially when it originates outside of your organization. This common wisdom is not always correct; the DeleteNoNDR method is useful more and more in these days of NDR spam attacks. If you are accepting mail for nonexistent recipients and generating NDRs, instead of refusing to accept them to begin with, then your SMTP queues are likely filling up with NDRs to forged senders, making your server contribute to the spam problem. If you are cleaning out stale messages of this sort in your queues, use the DeleteNoNDR method.

See Also

Recipe 7.12 for enumerating the queues, and Recipe 7.13 for listing the messages in a queue, MSDN: Exchange_QueuedMessage, MSDN: Exchange_QueuedSMTPMessage, and MSDN: Exchange_QueuedX400Message



Exchange Server Cookbook
Exchange Server Cookbook: For Exchange Server 2003 and Exchange 2000 Server
ISBN: 0596007175
EAN: 2147483647
Year: 2006
Pages: 235

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net