发送电子邮件使用PowerShell在Windows Server 2019中。

huangapple go评论75阅读模式
英文:

Send email with powershell in windows server 2019

问题

我使用脚本通过PowerShell发送邮件,通过Outlook:

Try{
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.To = ""
$mail.Subject = "Test from PS"
$mail.Display( $true )
$ol.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ol) | Out-Null
}
Catch{ write-host [ERROR] $Error }

在Windows 10中,它可以正常工作,但在Server 2019中,发送的消息仍然停留在发件箱中,只有在我明确打开Outlook时才会发送。

在脚本中是否有遗漏的部分,以强制Outlook立即发送生成的消息?

我已经在Outlook 2019和2021上进行了测试,在Outlook选项中,程序访问是'从不警告我有可疑活动'。

英文:

I use a script to send mail from powershell, through outlook:

Try{
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.To = ""
$mail.Subject = "Test from PS"
$mail.Display( $true )
$ol.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ol) | Out-Null
}
Catch{ write-host [ERROR] $Error }

It works correctly in windows 10, but in server 2019 the sent message remains in the outbox, and it is only sent if I enter outlook expressly.

Is there something missing from the script to force outlook to immediately send the generated message?

I have tested it with outlook 2019 and 2021
In outlook options, programmatic access is 'never warn me about suspicious activity'

答案1

得分: 0

以下是您要翻译的内容:

代码创建了一个新的Outlook邮件项,然后通过调用Display方法将其显示给用户:

$mail.Display( $true )

我想这在服务器端没有任何意义。

如果您需要发送邮件项,您需要调用Send方法,而不是Display方法。所以,代码可能如下所示:

Try{
$ol = New-Object -comObject Outlook.Application
$olNamespace = $ol.GetNamespace("MAPI")
$mail = $ol.CreateItem(0)
$mail.To = ""
$mail.Subject = "Test from PS"
$mail.Send()
$olNamespace.SendAndReceive(false);
$ol.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ol) | Out-Null
}
Catch{ write-host [ERROR] $Error }

此外,您可以尝试使用NameSpace.SendAndReceive方法,该方法启动立即传递当前会话中提交的所有未传递的消息,并立即接收当前配置文件中所有帐户的邮件。 SendAndReceive提供了与单击“工具”然后单击“发送/接收”时可用的“发送/接收全部”命令的编程等效功能。

如果不需要同步所有对象,您可以使用SyncObjects集合对象来选择特定的对象。有关更多信息,请参阅NameSpace.SyncObjects。等待同步过程完成然后才调用Quit方法关闭Outlook是有道理的。SyncObject.SyncEnd事件在Microsoft Outlook使用指定的“发送/接收”组完成同步用户文件夹后立即触发。

此外,请注意,Microsoft目前不建议也不支持从任何无人值守、非交互式客户端应用程序或组件(包括ASP、ASP.NET、DCOM和NT服务)自动化Microsoft Office应用程序,因为在此环境中运行Office时可能会表现出不稳定的行为和/或死锁。

如果您正在构建在服务器端上下文中运行的解决方案,您应该尝试使用已被设计为适用于无人值守执行的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方案。如果您在服务器端解决方案中使用Office应用程序,该应用程序将缺少使其成功运行所必需的许多功能。此外,您将冒着整体解决方案稳定性的风险。

阅读有关Office服务器端自动化的考虑文章以获取更多信息。

作为可能的解决方法,您可以考虑使用Graph API或EWS,请参阅使用Exchange中的EWS发送电子邮件消息以获取更多信息。

英文:

The code creates a new mail item in Outlook and then display it to a user by calling the Display method:

$mail.Display( $true )

Which I suppose doesn't make any sense on the server side.

If you need to send the mail item you need to call the Send method instead of Display one. So, the code may look like that:

Try{
$ol = New-Object -comObject Outlook.Application
$olNamespace = $ol.GetNamespace("MAPI")
$mail = $ol.CreateItem(0)
$mail.To = ""
$mail.Subject = "Test from PS"
$mail.Send()
$olNamespace.SendAndReceive(false);
$ol.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ol) | Out-Null
}
Catch{ write-host [ERROR] $Error }

Also you may try using the NameSpace.SendAndReceive method which initiates immediate delivery of all undelivered messages submitted in the current session, and immediate receipt of mail for all accounts in the current profile. SendAndReceive provides the programmatic equivalent to the Send/Receive All command that is available when you click Tools and then Send/Receive.

If you don't need to synchronize all objects, you can use the SyncObjects collection object to select specific objects. For more information, see NameSpace.SyncObjects. It makes sense to wait until the sync process is finished and only then close Outlook by calling the Quit method. The SyncObject.SyncEnd event is fired immediately after Microsoft Outlook finishes synchronizing a user's folders using the specified Send/Receive group.

Also be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

Read more about that in the Considerations for server-side Automation of Office article.

As a possible workaround you may consider using the Graph API or EWS, see Send email messages by using EWS in Exchange for more information.

huangapple
  • 本文由 发表于 2023年5月26日 16:18:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定