Professional Geek
RSS icon Email icon Bullet (black)
  • Clearing Exchange 2003 SMTP Queues

    Posted on April 22nd, 2008 AndyParkes 3 comments

    Earlier this week we had a client whose junior sales person decided to do a mail shot to some of their customer base. He wanted to send some attachments with information. The attachments totaled about 5mb in size (the default outgoing size limit had been changed at their request some time ago)

    So the sales guy clicked the send button and the 5mb message was submitted to exchange to send out to 150 recipients

    See the problem here? The exchange server was busy trying to send over 750MB of email which was stopping everything else from going out

    When I logged onto the server and viewed the queues I was a bit that annoyed that I’d have to go into every queue (one per domain), click “find messages”, then “find”, then delete all the offending messages. I knew there must be a better way but decided to look into it later as the problem needed sorting. It took me about half an hour but I cleaned up and all was well

    Until the day after.

    The same guy did exactly the same thing but to over 200 recipients. After late time I’d suggested he uploaded the attachments to his website and had a link in the email but that advice fell on deaf ears

    So I had no wish to go through all the queues again

    I found this post by Chrissy LeMaire most helpful. She has a vbscript that you can use to completely wipe out ALL the messages in ALL queues.

    I didn’t want to be quite so destructive as there were legitimate messages still in the queue so I made some changes so that the script asks you who sent the message and only deletes the specific messages. It also has a “report” mode which is it a bit like a “are you really sure” message. This will just let you know how many message would have been deleted

    Here is the script. I’ve marked my changes so that the script can be returned to it’s original state

    On reflection my changes are a bit rough and ready and when I get some more time I may change the WMI query so that it returns just the messages I want instead of returning all the messages and then checking to see which ones to delete though that’s just me being picky I guess

    Also take a look at Chrissy’s site. It’s very useful and has been added to my feed list!

    'Original Author: Chrissy LeMaire
    ' Copyright 2003 NetNerds Consulting Group
    ' Script is provided AS IS with no warranties or guarantees and assumes no liabilities.
    ' Website: http://www.netnerds.net
    ' Description: This scripts empties out the entire Exchange queue. USE WITH CAUTION.

    ' Modified by Andy Parkes - Changes indicated below
    ' www.andyparkes.co.uk

    ' Andy's new section 1
    dim txtemailAddress
    dim mailcounter
    dim deletemode
    mailcounter = 0
    deletemode="NO"
    txtemailAddress = LCASE(INPUTBOX("Please enter the email address SENDER to clear"))

    if txtemailAddress = "" then
    WScript.quit
    end if
    deletemode=MSGBOX("Click YES for DELETE MODE or NO for REPORT MODE",vbYesNo)
    ' End of new section 1

    Set objWMIExch = GetObject("winmgmts://./root/MicrosoftExchangeV2")
    Set objLinksList = objWMIExch.ExecQuery ("Select * from Exchange_SMTPLink")
    For each objLinkInst in objLinksList
    strSQL = "Select * from Exchange_SMTPQueue where "
    strSQL = strSQL & "LinkID='" & objLinkInst.LinkID
    strSQL = strSQL & "' and LinkName='" & objLinkInst.LinkName
    strSQL = strSQL & "' and ProtocolName='" & objLinkInst.ProtocolName
    strSQL = strSQL & "' and VirtualMachine='" & objLinkInst.VirtualMachine
    strSQL = strSQL & "' and VirtualServerName='" & objLinkInst.VirtualServerName & "'"
    Set objQueuesList = objWMIExch.ExecQuery (strsql)
    For each objQueueInst in objQueuesList
    i = i +1
    If i > 7 And InStr(objQueueInst.QueueName,".") > 0 Then 'make sure its not the built in stuff
    strSQL = "Select * from Exchange_QueuedSMTPMessage where " '<-- This class requires that you pass ALL the variables below in the where clause
    strSQL = strSQL & "LinkID='" & objLinkInst.LinkID
    strSQL = strSQL & "' and LinkName='" & objLinkInst.LinkName
    strSQL = strSQL & "' and ProtocolName='" & objLinkInst.ProtocolName
    strSQL = strSQL & "' and QueueID='" & objQueueInst.QueueID
    strSQL = strSQL & "' and QueueName='" & objQueueInst.QueueName
    strSQL = strSQL & "' and VirtualMachine='" & objLinkInst.VirtualMachine
    strSQL = strSQL & "' and VirtualServerName='" & objLinkInst.VirtualServerName & "'"
    Set objQueuesList1 = objWMIExch.ExecQuery (strsql)
    For each objQueueInst1 in objQueuesList1
    If i > 7 And InStr(objQueueInst1.QueueName,".") > 0 Then
    ' Andy's new section 2
    if LCASE(objQueueInst1.sender)=txtemailAddress then
    mailcounter = mailcounter + 1
    if deletemode=6 then
    objQueueInst1.DeleteNoNDR
    end if
    end if
    ' End of new section 2
    End If
    Next
    End If
    Next
    Next
    ' Andy's new section 3
    WScript.echo "Operation completed - " & i & " queues scanned - " & mailcounter & " messages processed"
    ' End of new section 3

     

    3 Responses to “Clearing Exchange 2003 SMTP Queues”

    1. When I try to run this script, it says
      line 19, char 24. Unterminated string constant
      According to my editor, line 19 is
      ” End of new section 1
      Nothing special here.
      The line below it is same as the original script while the one above it is:
      deletemode=MSGBOX(“Click YES for DELETE MODE or NO for REPORT MODE”,vbYesNo)
      Nothing special here either.

      Help?!
      Since I was in a hurry, I used Chrissy’s original code. I added a warning prompt to it for future use.

      Also, you should encapsulate your code. I had to replace all the quotation marks.

    2. PassiveX

      If i’ve just done a straight copy and paste from the browser i’m guessing there is some characters that have been converted incorrectly where the blog engine has converted them

      The line you mentioned

      ” End of new section 1

      That’s supposed to be a comment but they don’t look like valid comment characters. It should read

      ” End of new section 1

      I’ll think about the way i upload scripts from now. I don’t do it all that often and don’t usually expect them to be copied directly

    3. Just to confirm above comment

      I used Live Writer and plugin that is supposed to nicely format posted code

      Obviously it didn’t work! (which is why there was some CSS code at the bottom of the post)

      WordPress has a CODE XHTML TAG i sould have used

      it would have turned this:

      ”This is a test comment

      into this
      '' This is a test comment

      I’ve updated the post to reflect this but i’ll probably include scripts in an attached text file from now on

    Leave a Reply