C# outlook邮箱的Powershell脚本正在选择旧数据
我在这个项目中的电子邮件推到一个文件夹必须交叉检查是否到达。 因此,为了达到这一目的,我使用powershell脚本连接到邮件文件夹并收集数据。 代码粘贴在下面。C# outlook邮箱的Powershell脚本正在选择旧数据,c#,email,powershell,outlook,C#,Email,Powershell,Outlook,我在这个项目中的电子邮件推到一个文件夹必须交叉检查是否到达。 因此,为了达到这一目的,我使用powershell脚本连接到邮件文件夹并收集数据。 代码粘贴在下面。 Add-Type $class -ReferencedAssemblies Microsoft.Office.Interop.Outlook $class = @" using Microsoft.Office.Interop.Outlook;public class MyOL { public MAPIFolder GetI
Add-Type $class -ReferencedAssemblies Microsoft.Office.Interop.Outlook
$class = @"
using Microsoft.Office.Interop.Outlook;public class MyOL
{
public MAPIFolder GetInbox(string userName)
{
Application oOutlook = new Application();
NameSpace oNs = oOutlook.GetNamespace("MAPI");
Recipient oRep = oNs.CreateRecipient(userName);
MAPIFolder inbox = oNs.GetSharedDefaultFolder(oRep, OlDefaultFolders.olFolderInbox);
return inbox;
}
}
"@
Add-Type $class -ReferencedAssemblies Microsoft.Office.Interop.Outlook
$MyOL = New-Object MyOL
$olInbox = $MyOL.GetInbox("mailbox")
$olInbox.items | Select-Object -Property Subject, ReceivedTime, Importance, SenderName
但是这段代码只收集了一周前的数据而不是新的电子邮件。我确实试着四处看看,发现这可能与邮箱中的索引有关。因此,我也尝试过禁用它,但没有成功。不知道为什么不在Outlook中使用ComObject。如果您只想要最近的物品,则应获得过去24小时内收到的物品:
$MSOut = new-object -ComObject Outlook.Application
$MAPI = $MSOut.GetNamespace("MAPI")|?{$_.SMTP -match "Some.User@Company.com"}
$Account = $MAPI.Folders|?{$_.Name -match "Some.User@Company.com"}
$Inbox = $Account.folders | ?{$_.Name -match "Inbox"}
$24hours = (get-date).AddDays(-1)
$RecentItems = $Inbox.Items|?{$_.ReceivedTime -gt $24hours}
然后,如果您只想获得某些属性,可以针对该属性运行Select,如:
$RecentItems | Select -Property Subject, ReceivedTime, Importance, SenderName
这个代码不适合我!我是powershell脚本的新手。这与sdk的版本有关吗?您不需要sdk,只需安装Office即可。你从哪里得到错误?你给我的代码没有任何错误(谢谢),但它也没有检索任何数据。我粘贴的代码,当你执行它时,你会得到什么输出?它包含最新的电子邮件吗?如果是这样,那么这就是它应该工作的方式。我得到的回报是一周前的电子邮件列表:(.也就是说,当我今天在9号执行它时,我从2号收到了一个列表!我收到了过去24小时的电子邮件,正如预期的那样。我不确定当代码专门过滤过去24小时时,你怎么会收到一周前的电子邮件。你的Outlook是最新的吗?你打开Outlook时是否看到Outlook中列出了更多最近的电子邮件?你是否尝试过过滤usi你收到时间了吗?