none
Script to monitor mailbox RRS feed

  • Question

  • Hello Script Center,

    I'm looking for assistance with a script to monitor a particular mailbox. What I would like the script to do is search the mailbox and if there are any messages older than 10 minutes, send an email out. That's all, really.

    Thank you for any assistance.

    • Moved by Bill_Stewart Thursday, January 17, 2013 3:59 PM Move to more appropriate forum
    Thursday, January 17, 2013 3:51 PM

Answers

  • There are many methods that you can use to search Outlook items.  See the overview here:

    Searching Items:
    http://msdn.microsoft.com/en-us/library/ff869846(office.14).aspx

    I would run the search on a timer in an add-in of course, using the System.Timers.Timer class.


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Thursday, January 17, 2013 5:08 PM
    Moderator
  • It might not be a good idea to use the Timer class from the System.Timers namespace - it uses secondary threads to run the timer handler. And accessing Outlook Object Model from a secondary thread in an add-in will be nothing but trouble.

    Use the timer from the Forms namespace - it runs in the same thread used to create it.

     

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, January 17, 2013 5:41 PM
  • Or get the addin thread context and synch the call from the timer handler to the main thread context using SendOrPost callback and event handler: http://msdn.microsoft.com/en-us/library/system.threading..sendorpostcallback.aspx.
     
    I do that sometimes in addition to or instead of using a Windows.Forms timer.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Dmitry Streblechenko _MVP_" <=?utf-8?B?RG1pdHJ5IFN0cmVibGVjaGVua28gX01WUF8=?=> wrote in message news:a9777f71-0b4c-4458-8886-4a05d32061c7...

    It might not be a good idea to use the Timer class from the System.Timers namespace - it uses secondary threads to run the timer handler. And accessing Outlook Object Model from a secondary thread in an add-in will be nothing but trouble.

    Use the timer from the Forms namespace - it runs in the same thread used to create it.

     

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!


    Ken Slovak MVP - Outlook
    Thursday, January 17, 2013 6:04 PM
    Moderator
  • If you want a solution specific to Outlook, then you will need to rewrite your code to use the Outlook Object Model (and NOT run it in a scheduled task, which is unsupported).  System.Net.Mail is generally used on web servers for sending e-mail via SMTP, and has nothing to do with Outlook.  Questions on that namespace should be posted in the appropriate .NET forum.


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Thursday, January 17, 2013 7:28 PM
    Moderator

All replies

  • There are many methods that you can use to search Outlook items.  See the overview here:

    Searching Items:
    http://msdn.microsoft.com/en-us/library/ff869846(office.14).aspx

    I would run the search on a timer in an add-in of course, using the System.Timers.Timer class.


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Thursday, January 17, 2013 5:08 PM
    Moderator
  • It might not be a good idea to use the Timer class from the System.Timers namespace - it uses secondary threads to run the timer handler. And accessing Outlook Object Model from a secondary thread in an add-in will be nothing but trouble.

    Use the timer from the Forms namespace - it runs in the same thread used to create it.

     

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, January 17, 2013 5:41 PM
  • Or get the addin thread context and synch the call from the timer handler to the main thread context using SendOrPost callback and event handler: http://msdn.microsoft.com/en-us/library/system.threading..sendorpostcallback.aspx.
     
    I do that sometimes in addition to or instead of using a Windows.Forms timer.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Dmitry Streblechenko _MVP_" <=?utf-8?B?RG1pdHJ5IFN0cmVibGVjaGVua28gX01WUF8=?=> wrote in message news:a9777f71-0b4c-4458-8886-4a05d32061c7...

    It might not be a good idea to use the Timer class from the System.Timers namespace - it uses secondary threads to run the timer handler. And accessing Outlook Object Model from a secondary thread in an add-in will be nothing but trouble.

    Use the timer from the Forms namespace - it runs in the same thread used to create it.

     

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!


    Ken Slovak MVP - Outlook
    Thursday, January 17, 2013 6:04 PM
    Moderator
  • Thank you for all the input. I was looking to create a script and then run it as a scheduled task every day. After doing some preliminary research, I put together the following to test on my mailbox:

    #Script to monitor User Services mailbox
    
    #Search mailbox for any messages older than 10 minutes.
    #Emails Helpd Desk if these messages are present.
    
    [DateTime] $Today = Get-Date
    
    Search-Mailbox -Identity "hsitorus" -SearchQuery "Sent:> 
    
    $Today.addMinutes(-10)
    
    $emailFrom = "<sender>"
    $emailTo = "<recipient1>", "<recipient2>"
    $subject = "Mailbox"
    $body = "There are messages in the mailbox older than 10 minutes."
    $smtpServer = "smtp.company.com"
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $smtp.Send($emailFrom, $emailTo, $subject, $body)

    For some reason I'm receiving the following errors:

    The target mailbox or .pst file path is required.
        + CategoryInfo          : InvalidArgument: (:) [], ArgumentException
        + FullyQualifiedErrorId : 76432B43
    
    Exception calling "Send" with "4" argument(s): "The specified string is not in the form required for an e-mail address.
    "
    At C:\scripts\searchmailbox.ps1:18 char:11
    + $smtp.Send <<<< ($emailFrom, $emailTo, $subject, $body)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException


    Thursday, January 17, 2013 7:06 PM
  • If you want a solution specific to Outlook, then you will need to rewrite your code to use the Outlook Object Model (and NOT run it in a scheduled task, which is unsupported).  System.Net.Mail is generally used on web servers for sending e-mail via SMTP, and has nothing to do with Outlook.  Questions on that namespace should be posted in the appropriate .NET forum.


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Thursday, January 17, 2013 7:28 PM
    Moderator
  • Ken, Dmitry: it's still okay to use System.Timers.Timer in a Windows Service using Redemption, I assume.  From what I remember you can't use a Forms.Timer in a Windows Service for some reason.

    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Thursday, January 17, 2013 7:37 PM
    Moderator
  • reason is simple - someone has to pump messages on the thread where Forms timer live.

    Thursday, January 17, 2013 7:53 PM
  • Yes, Redemption will work fine on a secondary thread - you just need to make sure the MAPI system is initialized on that secondaery thread - create an instance of the RDOSession object and set its MAPIOBJECT property to Namespace.MAPIOBJECT from OOM.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, January 17, 2013 8:16 PM