Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
644 views
in Technique[技术] by (71.8m points)

imap - Get email using PowerShell

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 assebmlies, or MS Exchange direct work. Both are not appropriate.
How to use SMTP and send email - its absolutely clear, but how to receive? Isn't there any standard assemblies similar to System.Net.Mail?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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(“YourDirectoryimapx.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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...