POP3 Postfach mit PowerShell löschen

Written By Johannes Huber  |  Coding, PowerShell  |  5 Comments

Das Post Office Protocol (POP) ist ein Übertragungsprotokoll, über das ein Computer / Tablet /etc. E-Mails von einem E-Mail-Server abholen kann. POP3 (Version 3) ist in der Funktionalität sehr eingeschränkt und erlaubt nur das Auflisten, Abholen und Löschen von E-Mails. Für mehr Funktionalitäten müssen Protokolle wie IMAP verwendet werden. In einigen Anwendungsfällen kann es vorkommen, dass das Postfach automatisiert, bzw. regelmäßig gelöscht werden muss. Hier kommt folgendes PowerShell Script zum Einsatz. Wie man sein POP3 Postfach mit PowerShell löschen kann, seht ihr in folgendem Beitrag.

 

raphaelsilva / Pixabay

 

Das POP3 Postfach mit PowerShell löschen – So funktionierts!

Folgendes Script kommt ohne Drittanbierter – Tolls / DLLs aus.
Die Zeile 1 – 8 muss natĂĽrlich noch angepasst werden 🙂

[thrive_text_block color=”red” headline=”Vorsicht !”]

Vorsicht vor der Ausführung des PowerShell Scripts: Das Script löscht ALLE Mails im Postfach bzw. Mailbox.

[/thrive_text_block]

# POP HOST
$pop_host = "pop.yourmailserver.com"
# POP SSL PORT
$pop_port = 995
# POP USERNAME
$pop_username = "[email protected]"
# POP PASSWORD
$pop_password = 'yourpassword'


function Get-Response([string]$command){
    try{
        if ($command -ne ''){
            if ($client.Connected){
                $command = $command + "`r`n"
                $bytes = [System.Text.ASCIIEncoding]::ASCII.GetBytes($command)
                $ssl.Write($bytes,0,$bytes.length)
            }else{
                throw "TCP Verbindung getrennt."
            }
        }
        $ssl.Flush()
        $data = @()
        $sr = New-Object System.IO.StreamReader $ssl
        while ($sr.Peek() -gt -1){$data += $sr.ReadLine()}
        return $data
    }catch{
        throw $_.Exception.Message
    }
}


try{
    $client = New-Object System.Net.Sockets.TcpClient($pop_host,$pop_port)
    $ssl = New-Object System.Net.Security.SslStream($client.GetStream())
    $ssl.ReadTimeout = 20 * 1000
    $ssl.AuthenticateAsClient($pop_host)
    Get-Response ""
    Get-Response "USER $pop_username"
    Get-Response "PASS $pop_password"
    Get-Response "LIST" | ?{$_ -match '^(\d+) \d+'} | %{
        Get-Response "DELE $($matches[1])"
    }
    Get-Response "QUIT"

}catch{
    write-host $_.Exception.Message
}finally{
    $ssl.close()
    $ssl.Dispose()
    $client.Close()
    $client.Dispose()
}
  • Das ist ganz nett, aber auch gefährlich. Besser wäre noch eine Option “älter als x Tage”, sonst löscht das Script auch die Mail, die gerade reinkommt und noch keiner gesehen hat.

  • Ich bekomme von dem Script immer einen Fehler zurĂĽck, bekomme aber nicht raus woran es liegt.

    >> Exception calling “AuthenticateAsClient” with “1” argument(s): “Authentication failed because the remote party has closed the transport stream.”

    Ich hab schon die Netzwerkverbindung ĂĽber port 995 und auch den account samt Passwort vollständig geprĂĽft – da gibt es kein Problem. Mit einem Thunderbird zum Beispiel, kann ich die DB abfragen.

    Das Script aber schmeisst immer diesen Fehler.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >