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

PowerShell如何自动响应Outlook发送外部邮件时的安全确认弹窗

解决方案

方案1:直接使用SMTP发送邮件(推荐,无弹窗)

绕过Outlook COM对象的安全校验,直接调用.NET内置的SMTP类发信,不需要处理任何弹窗,兼容性更强。示例代码如下:

# 配置发信参数
$smtpServer = "你的企业SMTP服务器地址" # 可联系企业IT获取
$smtpPort = 587 # 常规端口为587或25,根据企业配置调整
$smtpUser = "你的企业邮箱地址"
$smtpPass = ConvertTo-SecureString "你的邮箱密码/应用专用密码" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($smtpUser, $smtpPass)

# 构造邮件
$mailParams = @{
    From = $smtpUser
    To = "receiver@gmail.com"
    Subject = "subject"
    Body = "testing"
    Attachment = "C:\Users\myusername\Desktop\data.csv"
    SmtpServer = $smtpServer
    Port = $smtpPort
    UseSsl = $true # 若企业SMTP无需SSL则设为$false
    Credential = $credential
}

# 发送邮件
Send-MailMessage @mailParams

该方案不依赖本地Outlook客户端,不会触发任何安全弹窗,是定时自动发信的最优选择。

方案2:自动点击Outlook安全弹窗(仅适合必须用Outlook发信的场景)

如果必须通过Outlook客户端发信,可以调用Windows UI自动化API查找弹窗窗口,模拟点击Allow按钮。示例代码如下:

# 加载UI自动化相关程序集
Add-Type -AssemblyName UIAutomationClient
Add-Type -AssemblyName UIAutomationTypes

# 原有Outlook发信逻辑
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "receiver@gmail.com" 
$Mail.Subject = "subject" 
$Mail.Body = "testing"
$Mail.Attachments.Add("C:\Users\myusername\Desktop\data.csv")

# 异步触发发信,避免脚本阻塞在发送步骤
Start-Job -ScriptBlock {
    param($Mail)
    $Mail.Send()
} -ArgumentList $Mail | Out-Null

# 等待弹窗出现,最长等待10秒
$maxWait = 10
$alertWindow = $null
for($i=0; $i -lt $maxWait; $i++){
    # 按弹窗标题查找窗口,需替换为你实际弹窗的标题(中文系统需替换为对应中文标题)
    $alertWindow = [Windows.Automation.AutomationElement]::RootElement.FindFirst([Windows.Automation.TreeScope]::Children, 
        [Windows.Automation.PropertyCondition]::new([Windows.Automation.AutomationElement]::NameProperty, "外部收件人提醒"))
    if($alertWindow -ne $null){
        break
    }
    Start-Sleep -Seconds 1
}

if($alertWindow -ne $null){
    # 查找Allow按钮,中文系统需替换为对应按钮文本比如"允许"
    $allowButton = $alertWindow.FindFirst([Windows.Automation.TreeScope]::Descendants,
        [Windows.Automation.PropertyCondition]::new([Windows.Automation.AutomationElement]::NameProperty, "Allow"))
    if($allowButton -ne $null){
        # 模拟点击按钮
        $invokePattern = $allowButton.GetCurrentPattern([Windows.Automation.InvokePattern]::Pattern)
        $invokePattern.Invoke()
    }
}

注意:

  • 弹窗标题、按钮文本需要和你实际看到的完全一致,否则会找不到对应元素
  • 部分企业的端点安全策略会禁止模拟点击操作,若执行后无响应请优先使用方案1

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 08:48:03