【发布时间】:2012-04-15 02:58:35
【问题描述】:
我只需要在 PowerShell 脚本中获取电子邮件并查看其主题 - 使用 pop3 或 imap,没关系。
我试图找到解决方案,但我发现的只是第 3 方 .net 组件或 MS Exchange 直接工作。两者都不合适。
如何使用 SMTP 和发送电子邮件 - 非常清楚,但如何接收?没有类似 System.Net.Mail 的标准程序集吗?
【问题讨论】:
标签: powershell imap pop3
我只需要在 PowerShell 脚本中获取电子邮件并查看其主题 - 使用 pop3 或 imap,没关系。
我试图找到解决方案,但我发现的只是第 3 方 .net 组件或 MS Exchange 直接工作。两者都不合适。
如何使用 SMTP 和发送电子邮件 - 非常清楚,但如何接收?没有类似 System.Net.Mail 的标准程序集吗?
【问题讨论】:
标签: powershell imap pop3
我使用了 Falah Abu Hassan 的建议,它非常适合我通过 IMAP 接收邮件的要求!
如何获取 IMAPX.DLL
imapx 的 Github 存储库位于此处: https://github.com/azanov/imapx
不幸的是,您必须使用“Visual Studio”自己编译才能获得 imapx.dll。
创建示例 Powershell 脚本
Script 和 DLL 应该放在一边,并且可以与此集成:
$path = Split-path $script:MyInvocation.MyCommand.Path
[Reflection.Assembly]::LoadFile(“$path\imapx.dll”)
以下示例脚本的灵感来自 Falah Abu Hassan 的回答,对我来说效果很好:
$path = Split-path $script:MyInvocation.MyCommand.Path
[Reflection.Assembly]::LoadFile(“$path\imapx.dll”)
### Create a client object
$client = New-Object ImapX.ImapClient
$client.Behavior.MessageFetchMode = "Full"
$client.Host = "Servername"
$client.Port = 993
$client.UseSsl = $true
$client.IsDebug = $true
$client.ValidateServerCertificate = $true
$client.Connect()
$user = "login@domain"
$pass = 'password'
$client.Login($user, $pass)
$messages = $client.Folders.Inbox.Search("ALL", $client.Behavior.MessageFetchMode, 100)
write-host "Count found: $($messages.count)"
foreach($m in $messages){
write-host "Processing Subject: $($m.Subject)"
}
【讨论】:
这是我在 c# 上使用的代码。我已将 dll 导入到 powershell 并使用它来检索消息的不同部分。我使用的 dll 是开源的 Imapx2。我了解您不想使用第三方 .NET 程序集,但这可能会帮助其他尝试访问此内容的人。
### 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"
}
}
希望对你有帮助
【讨论】: