You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PowerShell借助外部HTML文件发送Outlook邮件?

解决PowerShell读取外部HTML文件作为Outlook邮件正文的问题

问题核心在于Get-Content默认返回字符串数组(文件每行对应一个数组元素),而Outlook的HTMLBody属性需要接收单个完整的HTML字符串,直接赋值数组会导致无法解析,最终邮件正文为空。

方法1:使用Get-Content的-Raw参数

-Raw参数会将整个文件内容读取为单个完整字符串,完全适配HTMLBody的要求:

$HTMLPath = "C:\Users\username\htmlfolder\outlookmessage.html"
# 读取完整HTML内容为单个字符串
$body = Get-Content $HTMLPath -Raw
# 赋值给邮件HTMLBody属性
$mail.HTMLBody = $body

方法2:使用.NET File.ReadAllText方法

若需兼容较旧版本的PowerShell,可直接调用.NET类读取完整文本:

$HTMLPath = "C:\Users\username\htmlfolder\outlookmessage.html"
$body = [System.IO.File]::ReadAllText($HTMLPath)
$mail.HTMLBody = $body

额外需求:在外部HTML中动态替换变量

如果HTML文件需要替换动态变量(比如原Here-String中的$Example),可先读取模板内容,再通过替换逻辑填充变量:

$Example = "动态生成的标题内容"
$HTMLPath = "C:\Users\username\htmlfolder\outlookmessage.html"
# 读取HTML模板
$template = Get-Content $HTMLPath -Raw
# 替换模板中的占位符(需提前在HTML中用{{Example}}这类标记占位)
$body = $template -replace "{{Example}}", $Example
$mail.HTMLBody = $body

内容的提问来源于stack exchange,提问作者tmccaffrey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 22:01:36