c# - 如何在 PowerShell 中创建一个 Outlook 规则,将电子邮件无错误地移动到文件夹,使用在 C# 中完美运行的相同代码?

标签 c# powershell email outlook office-interop

我正在尝试编写 PowerShell 代码来创建 Outlook 规则来移动电子邮件,但它不起作用。

请注意,我无权访问服务器,因此 *-InboxRule cmdlet(如 New-InboxRule)不可用。

PowerShell 和 Outlook 之间的 COM 互操作发生了一些奇怪的事情,因为它在 C# 中完美运行,但相同的代码在 PowerShell 中却不行。

这是 C# 代码,完美:

Microsoft.Office.Interop.Outlook.Application outlook = null;

try
{
    outlook = (Microsoft.Office.Interop.Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
}
catch
{
}

if (outlook == null)
{
    outlook = new Microsoft.Office.Interop.Outlook.Application();
}

var inbox = outlook.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
var oMoveTarget = inbox.Folders["MoveTarget"];

// debugging
Console.WriteLine(inbox.FolderPath.ToString());
Console.WriteLine(oMoveTarget.FolderPath.ToString());

var rules = outlook.Session.DefaultStore.GetRules();
Console.WriteLine(string.Format("There are {0} rules", rules.Count));

var name = string.Format("Rule {0}", DateTime.Now.ToString("yyyyMMdd_HHmmss"));
var rule = rules.Create(name, Microsoft.Office.Interop.Outlook.OlRuleType.olRuleReceive);

// conditions
rule.Conditions.From.Recipients.Add("John Smith");
rule.Conditions.From.Recipients.ResolveAll();
rule.Conditions.From.Enabled = true;

// actions
rule.Actions.MoveToFolder.Folder = oMoveTarget;
rule.Actions.MoveToFolder.Enabled = true;
rules.Save(true);

现在,这是完全相同(语言语法除外)的 PowerShell 代码:

try
{
    $outlook = [Runtime.InteropServices.Marshal]::GetActiveObject("Outlook.Application");
}
catch
{
}

if ($outlook -eq $null)
{
    $outlook = New-Object -ComObject Outlook.Application
}

$inbox = $outlook.Application.Session.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox);
$oMoveTarget = $inbox.Folders["MoveTarget"];

# debugging
[Console]::WriteLine($inbox.FolderPath.ToString());
[Console]::WriteLine($oMoveTarget.FolderPath.ToString());

$rules = $outlook.Session.DefaultStore.GetRules();
[Console]::WriteLine([string]::Format("There are {0} rules", $rules.Count));

$name = [string]::Format("Rule {0}", [DateTime]::Now.ToString("yyyyMMdd_HHmmss"));
$rule = $rules.Create($name, [Microsoft.Office.Interop.Outlook.OlRuleType]::olRuleReceive);

# conditions
$rule.Conditions.From.Recipients.Add("John Smith");
$rule.Conditions.From.Recipients.ResolveAll();
$rule.Conditions.From.Enabled = $true;

# actions
$rule.Actions.MoveToFolder.Folder = $oMoveTarget;
$rule.Actions.MoveToFolder.Enabled = $true;
$rules.Save($true);

虽然 C# 代码成功,但 PS 代码失败:

One or more rules cannot be saved because of invalid actions or conditions.
At line:1 char:1
+ $rules.Save($true);
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
 

进一步调查,这基本上是由这条线引起的,它实际上没有做任何事情:

$rule.Actions.MoveToFolder.Folder = $oMoveTarget;

当我运行 C# 等效程序时,如果我立即转身查看该属性,它已设置。但是,在 PowerShell 中,它不会抛出错误或任何东西...它只是默默地什么都不做

我怎样才能让它发挥作用?请帮忙!

最佳答案

你可以这样做:

# actions
$action = $rule.Actions.MoveToFolder
$action.Enabled = $true

[Microsoft.Office.Interop.Outlook.MoveOrCopyRuleAction].InvokeMember("Folder",[Reflection.BindingFlags]::SetProperty, $null, $action, $oMoveTarget) 

关于c# - 如何在 PowerShell 中创建一个 Outlook 规则,将电子邮件无错误地移动到文件夹,使用在 C# 中完美运行的相同代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67222338/

相关文章:

azure - 无法通过端口 445 访问 Azure 存储帐户

PHP 邮件表单不会发送电子邮件字段中的空格

javascript - 带有联系人下拉菜单的联系表格

perl - 如何通过 SSL 下载 IMAP 邮件附件并使用 Perl 将它们保存在本地?

c# - IDBConnection应该放在哪里,减少重复代码?

c# - 在 ASP.NET MVC 5 Controller 中使用 POST 从 dotnet Core Web API 下载文件

xml - 比较两个文件列表及其内容

c# - .NET 值类型在内存中的布局

c# - 使用 net.tcp 绑定(bind)在 IIS 7 上托管 WCF 服务 - 使用服务时出错

powershell - 什么会导致[Environment]::SetEnvironmentVariable静默失败?