View Single Post
Old 02-25-2008   #1 (permalink)
Jacob Saaby Nielsen


 
 

Reading Outlook Messages

Hey guys,

I'm doing this script, and as such it works ok. However, the script is counting
766 mails that match the subject filter,
and I'm only getting 247 e-mail addresses from the script.

So I poked around a bit, and noticed that a lot of the e-mail messages has
a "This item cannot be displayed in the reading
pane". Seems when I run the script, Outlook reaches a point where I have
too many open messages at the same time.

Anybody got a take on how to get around this ?

I'm pasting my sourcecode so you have the option of commenting my code, what
to improve, or specifically how to get
around this "problem".

# ================================================================================================================
#
# Script Information
#
# Title: NDRs.ps1
# Author: Jacob Saaby Nielsen ()
# Originally created: 06-02-2008 - 11:25:18
# Description: Extracts e-mail addresses and error-codes from NDR mails,
from a folder in Outlook
#
# ================================================================================================================

#-------- Configurable Settings --------

# $subjectFilter needs to contain the word/words of the subject you want
to match
$subjectFilter = "User Forum"

# $NDRFolderName is the name of the folder in which you store the NDRs. Folder
needs to be at the folderlevel just beneath Inbox
$NDRFolderName = "Undelivered"

# $NDRResultsFolder is the folder in which you want your results file located.
End with a backslash.
$NDRResultsFolder = "c:\"

# $NDRResultsFile is the name of the results file
$NDRResultsFile = "ndr-list.csv"


#-------- Non-configurable Settings - Do not change anything after this line
--------

# Instantiating Outlook etc.
$olFolderInbox = 6;
$outlook = New-Object -com outlook.application;
$ns = $outlook.GetNameSpace("MAPI");
$NDRs = $ns.GetDefaultFolder($olFolderInbox).Folders.Item("$NDRFolderName")

CLS
Write-Host 'Getting total number of eAlert mails...'

# Filter out all the files that are not meeting your subject filter
$eAlertMails = $NDRs.Items | where {$_.Subject -match $subjectFilter}

# Returns e-mail address from the message body, based on a regular expression
function get-Email([string]$MailBody)
{
$Regex = [regex] "([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})"
$Match = $Regex.Match($MailBody)
return $Match.Value
}

[int]$MailCounter = 1

foreach ($eAlertMail in $eAlertMails.Items)
{
# Create and update the progress bar
$status = "Processing mail {0} of {1}" -f $MailCounter, $eAlertMails.Count
Write-Progress $status -PercentComplete ((100 / $eAlertMails.Count) * $MailCounter
) -Activity "Processing e-mails" -ID 1

# Add the string to the results file
Add-Content -path $NDRResultsFolder$NDRResultsFile -value (get-Email $eAlertMail.Body)

# Increment $MailCounter
$MailCounter++
}
Best Regards,
Jacob Saaby Nielsen

http://www.pipforhelvede.net
gmail: jacob DOT saaby
hotmail (IM/LinkedIN/Facebook): same as gmail


My System SpecsSystem Spec