# IMAP PDF attachment downloader (requires MailKit DLL)

# Először töltsd le a MailKit DLL-t: https://www.nuget.org/packages/MailKit
# Példa letöltés: https://github.com/jstedfast/MailKit/releases/latest

Add-Type -Path "C:\Apps\MailKit\MailKit.dll"
Add-Type -Path "C:\Apps\MailKit\MimeKit.dll"

$imapServer = "your.exchange.server"
$imapPort = 993
$username = "your-username"
$password = "your-password"

$client = New-Object MailKit.Net.Imap.ImapClient
$client.Connect($imapServer, $imapPort, $true)
$client.Authenticate($username, $password)
$inbox = $client.Inbox
$inbox.Open([MailKit.FolderAccess]::ReadOnly)

foreach ($msg in $inbox.Fetch(0, $inbox.Count - 1, [MailKit.MessageSummaryItems]::Full | [MailKit.MessageSummaryItems]::UniqueId)) {
    $email = $inbox.GetMessage($msg.UniqueId)
    foreach ($attachment in $email.Attachments) {
        if ($attachment.ContentType.MediaType -eq "application" -and $attachment.ContentType.MediaSubtype -eq "pdf") {
            $filePath = "C:\Downloads\" + $attachment.FileName
            $stream = [System.IO.File]::Create($filePath)
            $attachment.Content.DecodeTo($stream)
            $stream.Close()
            Write-Host "Downloaded: $filePath"
        }
    }
}

$client.Disconnect($true)