# Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
## Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$downloadTempDirectory = "C:\EMailDownloadTempDirectory"
## Create Exchange Service Object
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
$service.UseDefaultCredentials = $true
$service.AutodiscoverUrl("[email protected]",{$true})
#Bind to Inbox
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox, "[email protected]" ) ## $MailboxName)
$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
#Define the properties to get
$psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
#Define ItemView to retrive just 1000 Items
$ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)
#Get the ID of the folder to move to
$fvFolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
$fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep; ## Shallow;
$SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,"abgearbeitet")
$findFolderResults = $Inbox.FindFolders($SfSearchFilter,$fvFolderView)
$fiItems = $service.FindItems($Inbox.Id,$ivItemView)
if( $fiItems -ne $NULL ) {
[Void]$service.LoadPropertiesForItems($fiItems,$psPropset)
foreach($Item in $fiItems.Items){
"RecivedDate : " + $Item.DateTimeReceived
"Subject : " + $Item.Subject
## "Size : " + $Item.Size
"Recipients : " + $Item.ToRecipients
## "Sender : " + $Item.Sender
$Item.ToRecipients.Address -match "(\S+)eingang" | Out-Null
$emailsendto = $matches[1] + "@meinedomain.de"
$Item.Load()
foreach( $attach in $Item.Attachments ) {
$attach.Load()
$fiFile = new-object System.IO.FileStream(($downloadTempDirectory + “\” + $attach.Name.ToString()), [System.IO.FileMode]::Create)
$fiFile.Write($attach.Content, 0, $attach.Content.Length)
$fiFile.Close()
$ifilenamep = $downloadTempDirectory+"\"+$attach.Name.ToString()
$ofilenamep = $downloadTempDirectory+"\"+$attach.Name.ToString().replace(".pdf","") + "-bearbeitet.pdf"
write-host "Downloaded Attachment :" $ifilenamep
$attach.Name.ToString() -cmatch "(\d\d\.\d\d\.\d\d)_(\d\d\.\d\d).*\.(\S+).pdf" | Out-Null
$indate = $matches[1]
$intime = $matches[2]
$innr = $matches[3]
Write-Host "Datum : " $indate ", Uhrzeit : " $intime ", Nummer :" $innr ", SendeAn: " $emailsendto
Start-Process "C:\Program Files\gs\gs9.56.1\bin\gswin64c.exe" -wait -workingdirectory "C:\Program Files\gs\gs9.56.1\bin" -ArgumentList "ganz viele geheimnisvolle Dinge passieren hier ..."
Remove-Item $ifilenamep -Force
Rename-Item $ofilenamep -NewName $ifilenamep -Force
# Create Email Object
$EmailMessage = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service
#Set the Subject
$EmailMessage.Subject = "empfangen vom " + $indate + " um " + $intime + " von Nummer " + $innr
#Set the Body of Messsage
$EmailMessage.Body = "Die Daten befinden sich im Anhang"
#Add Recipients
$EmailMessage.ToRecipients.Add($emailsendto)
#Add Attachment
$EmailMessage.Attachments.AddFileAttachment($ifilenamep) | Out-Null
#Send Message SendandSave will save the Message to the Sender Sent Item Folder
$EmailMessage.SendAndSaveCopy() | Out-Null
Remove-Item $ifilenamep -Force
}
$Item.Move($findFolderResults.Folders[0].Id) | Out-Null
}
}