2012-03-31 111 views
2

我需要的是在PoSh脚本中获取电子邮件,并查看其主题 - 使用pop3或imap,无关紧要。
我试图找到解决方案,但是我发现所有解决方案都是第三方.net组件或MS Exchange直接工作。两者都不合适。
如何使用SMTP和发送电子邮件 - 其绝对清晰,但如何接收?没有类似于System.Net.Mail的标准组件吗?使用PowerShell获取电子邮件

+0

可能要检查http://stackoverflow.com/questions/26606/free-pop3-net-library – 2012-04-01 03:43:34

回答

1

根据你的目标,NetCmdlets将是一个很好的适合恕我直言。

http://www.powershellinside.com/powershell/netcmdlets/

您会注意到IMAP和POP的支持。

+0

正如我在写的问题 - 我见过几个包含第三方.NET程序集的解决方案,包括商业版本,但不合适。问题是 - 是不是有类似于smtp System.Net.Mail的标准解决方案? – dmitry 2012-04-01 12:44:06

+0

啊,呃。 BCL没有这个,对不起。 :( – 2012-04-01 16:59:01

+0

FWIW,因为问题说第三方程序集,我希望cmdlet可能对你更好。) – 2012-04-01 17:00:41

4

这是我在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" 
    } 
} 

我希望这是有帮助的

相关问题