Recipe 7.14. Deleting Messages from a QueueProblemYou need to delete messages from a queue on your Exchange virtual server. SolutionUsing a graphical user interface
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 DiscussionThis 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.
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 AlsoRecipe 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 |