none
EWS or Powershell script to send notification of unread emails to their private emails for unmonitored user mailboxes. RRS feed

  • Question

  • Hi Team,

    We have few users who do not check their mailboxes regularly. May I request a script which will send a information email of unread email count to their private email address.

    I found few below articles which gives only unread email count, can we add an additional function so that it will send notification email to a given list 

    https://smtpport25.wordpress.com/2009/08/24/powershell-to-get-the-number-of-mails-in-the-inbox-and-number-of-unread-emails-in-inbox/  http://www.infinitec.de/post/2011/07/25/How-to-get-the-number-of-unread-mails-from-multiple-mailboxes-using-the-EWS-Managed-API-and-PowerShell.aspx

    Thank you.


    Ahmed Ali

    Monday, September 7, 2015 8:46 AM

Answers

  • If you use http://www.infinitec.de/post/2011/07/25/How-to-get-the-number-of-unread-mails-from-multiple-mailboxes-using-the-EWS-Managed-API-and-PowerShell.aspx

    just modify

    function ProcessResult($entry) 
    {
        "Mailbox: {0}" -f $entry.EmailAddress
        "Unread Mails: {0}" -f $entry.UnreadMailCount
        "==============================================================================="
    }
    
    Mark has shown one way of sending an Email using the Send-MailMessage EMS cmdlet otherwise you can use EWS to send it instead eg

    function ProcessResult($entry)
    {
        "Mailbox: {0}" -f $entry.EmailAddress
        "Unread Mails: {0}" -f $entry.UnreadMailCount
        "==============================================================================="$EmailMessage = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service $EmailMessage.Subject = "Unread Mails: {0}" -f $entry.UnreadMailCount #Add Recipients $EmailMessage.ToRecipients.Add("user@domain.com") $EmailMessage.Body = New-Object Microsoft.Exchange.WebServices.Data.MessageBody $EmailMessage.Body.BodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::HTML $EmailMessage.Body.Text = "Unread Mails: {0}" -f $entry.UnreadMailCount $EmailMessage.SendAndSaveCopy()

    }

    • Marked as answer by AhmedShaik Monday, September 14, 2015 2:53 PM
    Tuesday, September 8, 2015 2:30 AM
  • It sounds like there is problem with the input.txt file your using eg it doesn't have the correct header (MailAddress) therefor the following lines aren't doing anything

    $addresses = Import-Csv $inputFile 
    $addresses | % {GetUnreadInboxMails($_.MailAddress)}  | % {ProcessResult ($_)}
    

    It should be really basic for you to debug https://technet.microsoft.com/en-us/library/ee176874.aspx eg make sure you can first iterate through the file or test the rest of the code manually first just with one email eg

    GetUnreadInboxMails(Mailboxtocheck@domain)  | % {ProcessResult ($_)}

    Cheers
    Glen

    • Marked as answer by AhmedShaik Monday, September 14, 2015 2:53 PM
    Wednesday, September 9, 2015 4:36 AM

All replies

  • Try this. It'll look at the last 24hrs of message tracking logs to find the number of emails sent to each recipient in a list then email them the number emails received that day:

    $recipients = "user1@company.com","user2@company.com"
    $HTServers = "server1","server2"
    $smtp = "smtp.company.com"
    $from = "EmailAlerts@company.com"
    
    foreach($recipient in $recipients)
        {
            
            $startdate = [datetime]::Now.AddDays(-1)
            $enddate = [datetime]::Now
            $emails = @()
        
            foreach($HTServer in $HTServers)
                {
                    $emails += Get-MessageTrackingLog -Start $startdate -End $enddate -ResultSize Unlimited -Server $HTServer | ? {$_.Recipients -match $recipient}
                }
    
            $newemails = $emails | select messageid -Unique
            $count = ($newemails).Count
            if(!$count -and $newemails)
                {
                    $count = 1
                }
            if(!$newemails)
                {
                    $count = 0
                }
    
            $subject = $count.ToString() + " new emails received today"
            Send-MailMessage -To $recipient -From $from -Subject $subject -SmtpServer $smtp
    
        }
    

    If you do still want the unread message count then you'll need to use EWS instead of the Outlook PowerShell script because EWS won't rely on any client machine or Outlook.

    Let me know if this answers your question. 


    Mark Gossa

    MCSE 2003, MCITP Enterprise Administrator 2008 R2, MCSA 2012 R2, MCTS Exchange 2010

    Blog: http://markgossa.blogspot.com

    Posts are provided “AS IS” without warranty of any kind, either expressed or implied.

    Monday, September 7, 2015 12:41 PM
  • Hi Mark,

    This is an good idea but we our requirement is specifically to get the unread item count. 

    Thank you.



    Ahmed Ali

    Monday, September 7, 2015 1:05 PM
  • If you use http://www.infinitec.de/post/2011/07/25/How-to-get-the-number-of-unread-mails-from-multiple-mailboxes-using-the-EWS-Managed-API-and-PowerShell.aspx

    just modify

    function ProcessResult($entry) 
    {
        "Mailbox: {0}" -f $entry.EmailAddress
        "Unread Mails: {0}" -f $entry.UnreadMailCount
        "==============================================================================="
    }
    
    Mark has shown one way of sending an Email using the Send-MailMessage EMS cmdlet otherwise you can use EWS to send it instead eg

    function ProcessResult($entry)
    {
        "Mailbox: {0}" -f $entry.EmailAddress
        "Unread Mails: {0}" -f $entry.UnreadMailCount
        "==============================================================================="$EmailMessage = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service $EmailMessage.Subject = "Unread Mails: {0}" -f $entry.UnreadMailCount #Add Recipients $EmailMessage.ToRecipients.Add("user@domain.com") $EmailMessage.Body = New-Object Microsoft.Exchange.WebServices.Data.MessageBody $EmailMessage.Body.BodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::HTML $EmailMessage.Body.Text = "Unread Mails: {0}" -f $entry.UnreadMailCount $EmailMessage.SendAndSaveCopy()

    }

    • Marked as answer by AhmedShaik Monday, September 14, 2015 2:53 PM
    Tuesday, September 8, 2015 2:30 AM
  • Hi Glen,

    Modified and used the EWS script. I neither get an error nor the result. Below is the output while executing the script.

    PS C:\temp> .\Inboxcount_EWS.ps1 c:\temp\input.txt $credential

    GAC    Version        Location
    ---    -------        --------
    False  v2.0.50727     C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll


    Ahmed Ali

    Tuesday, September 8, 2015 5:01 PM
  • It sounds like there is problem with the input.txt file your using eg it doesn't have the correct header (MailAddress) therefor the following lines aren't doing anything

    $addresses = Import-Csv $inputFile 
    $addresses | % {GetUnreadInboxMails($_.MailAddress)}  | % {ProcessResult ($_)}
    

    It should be really basic for you to debug https://technet.microsoft.com/en-us/library/ee176874.aspx eg make sure you can first iterate through the file or test the rest of the code manually first just with one email eg

    GetUnreadInboxMails(Mailboxtocheck@domain)  | % {ProcessResult ($_)}

    Cheers
    Glen

    • Marked as answer by AhmedShaik Monday, September 14, 2015 2:53 PM
    Wednesday, September 9, 2015 4:36 AM