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

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.

September 7th, 2015 4:47am

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. 

Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 8:42am

Hi Mark,

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

Thank you.


September 7th, 2015 9:06am

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()

}

Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 10:31pm

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

September 8th, 2015 1:02pm

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

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 12:36am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics