Powershell - .EML Dateien per SMTP Weiterleiten
antony kann über den “Sicherungspfad” eingehende E-Mails als .EML Datei im Dateisystem speichern. Mit diesen Script ist es möglich (z.B. über die Aufgabenplanung) diese Mails dann an andere E-Mailadressen zu schicken.
function Load-NugetAssembly {
[CmdletBinding()]
param(
[string]$url,
[string]$name,
[string]$zipinternalpath,
[switch]$downloadonly
)
if($psscriptroot -ne ''){
$localpath = join-path $psscriptroot $name
}else{
$localpath = join-path $env:TEMP $name
}
$tmp = "$env:TEMP\$([IO.Path]::GetRandomFileName())"
$zip = $null
try{
if(!(Test-Path $localpath)){
Add-Type -A System.IO.Compression.FileSystem
write-host "Downloading and extracting required library '$name' ... " -F Green -NoNewline
(New-Object System.Net.WebClient).DownloadFile($url, $tmp)
$zip = [System.IO.Compression.ZipFile]::OpenRead($tmp)
$zip.Entries | ?{$_.Fullname -eq $zipinternalpath} | %{
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_,$localpath)
}
Unblock-File -Path $localpath
write-host "OK" -F Green
}
if(!$downloadonly.IsPresent){
Add-Type -Path $localpath -EA Stop
}
}catch{
write-host ($_.Exception.LoaderExceptions | fl | out-string)
throw "Error: $($_.Exception.Message)"
}finally{
if ($zip){$zip.Dispose()}
if(Test-Path $tmp){del $tmp -Force}
}
}
# load required assemblies if not already loaded
if (!('MailKit.Net.Smtp.SmtpClient' -as [type])){
Load-NugetAssembly 'https://www.nuget.org/api/v2/package/Portable.BouncyCastle/1.8.8' -name 'BouncyCastle.Crypto.dll' -zipinternalpath 'lib/net40/BouncyCastle.Crypto.dll' -EA Stop
Load-NugetAssembly 'https://www.nuget.org/api/v2/package/System.Buffers/4.5.1' -name 'System.Buffers.dll' -zipinternalpath 'lib/net461/System.Buffers.dll' -EA Stop
Load-NugetAssembly 'https://www.nuget.org/api/v2/package/MimeKit/2.10.1' -name 'MimeKit.dll' -zipinternalpath 'lib/net45/MimeKit.dll' -EA Stop
Load-NugetAssembly 'https://www.nuget.org/api/v2/package/MailKit/2.10.1' -name 'MailKit.dll' -zipinternalpath 'lib/net45/MailKit.dll' -EA Stop
}
# folder with eml files
$folder = "C:\antonyEmlExport\postfach"
$options = $null; $smtp = $null
try{
$options = New-Object MimeKit.ParserOptions
$smtp = New-Object MailKit.Net.Smtp.SmtpClient
$smtp.SslProtocols = 'Tls11','Tls12','Tls13'
$smtp.Connect("**SMTPSERVER**",25,$false)
$smtp.Authenticate("**benutzername**",'**geheim**')
}catch{
Write-Error -Message $_.Exception.Message
return
}
# for each *.eml in folder
foreach($file in Get-ChildItem $folder -File -Filter *.eml){
try{
write-host "Sending mail '$($file.Fullname)' ... " -F Green -NoNewline
$msg = [MimeKit.MimeMessage]::Load($options,$file.Fullname)
# (optional) to add additional recipients (CC/BCC/TO) to the mails you can use the following scheme (uncomment and change recipient)
# $msg.BCC.Add('user@domain.tld')
# $msg.CC.Add('user@domain.tld')
# $msg.TO.Add('user@domain.tld')
$msg.TO.clear()
$msg.TO.Add('neuer@meinedomaene.de')
# send message
$smtp.Send($msg)
write-host "OK" -F Green
Remove-Item -Path $file.Fullname
}catch{
Write-Error -Message "Error loading or sending message: $($_.Exception.Message)"
}
}
# cleanup
if ($smtp){
$smtp.Disconnect($true)
$smtp.Dispose()
}