Get email using PowerShell

前端 未结 2 1503
你的背包
你的背包 2020-12-17 05:43

All I need is get email in PowerShell Script and see at its topic - with pop3 or imap, doesnt matter.
I tried to find solution, but all I found is either 3rd party .net

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-17 06:10

    Here is a code I have been using on c#. I have Imported the dll to powershell and used it to retrieve different parts of a message. The dll I used is Imapx2 which is an open source. I understand that you don't want to use a third party .NET assemblies but this might help other people trying to reach to this content.

    ### Import the dll
    [Reflection.Assembly]::LoadFile(“YourDirectory\imapx.dll”)
    ### Create a client object
    $client = New-Object ImapX.ImapClient
    ###set the fetching mode to retrieve the part of message you want to retrieve, 
    ###the less the better
    $client.Behavior.MessageFetchMode = "Full"
    $client.Host = "imap.gmail.com"
    $client.Port = 993
    $client.UseSsl = $true
    $client.Connect()
    $user = "User"
    $password = "Password"
    $client.Login($user,$password)
    $messages = $client.Folders.Inbox.Search("ALL", $client.Behavior.MessageFetchMode, 1000)
    foreach($m in $messages){
    $m.Subject
    foreach($r in $m.Attachments){
    $r | Out-File "Directory"
        }
     }
    

    I hope this was helpful

提交回复
热议问题