Powershell、Outlook、签名

Powershell, Outlook, Signature

获取此代码以在 outlook 中创建一个新电子邮件以从特定帐户发送并且它完美运行。

现在,只要我向其中添加“$Mail.HTMLBody = $Msg”,它就会删除签名,而我所拥有的只是空白文本。

对我来说,添加“$Msg”似乎可以替换整个 HTLM 正文。

有没有一种方法可以将文本添加到现有的 html 电子邮件正文中,或者有一种方法可以将正确的签名重新添加到电子邮件中?

$Msg = "<span style='font-family:Calibri;font-size:12pt;'>Sehr geehrte Damen und Herren</span>"

$Outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$namespace.Logon($null, $null, $false, $true)
$EmailFrom = ('test@test.com')
$account = $outlook.Session.Accounts.Item($EmailFrom)
$Mail = $Outlook.CreateItem(0)
#$Mail.HTMLBody = $Msg

这对我有用。我不确定注册表项是否适合您,或者是否需要进行一些修改

$Outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace('MAPI')

# Get the Outlook profile name to use for looking up the signature name in the registry
$currentProfile = $Outlook.Session.CurrentProfileName

# Check the registry for the signature name to use for New mails
$reg = "HKCU:\SOFTWARE\Microsoft\Office.0\Outlook\Profiles$currentProfile75CFF0413111d3B88A00104B2A6676[=10=]000002"
$signatureName = (Get-ItemProperty $reg -Name 'New Signature' ).'New Signature'

# Find the cooresponding htm signature file in appdata
$signatureFile = Get-Item "${env:APPDATA}\Microsoft\Signatures${signatureName}.htm"

# Get the contents of the $signature file
$signature = Get-Content $signatureFile -Raw

$mail = $Outlook.CreateItem(0)

# Add the signature to end of the message
$Msg = "<span style='font-family:Calibri;font-size:12pt;'>Sehr geehrte Damen und Herren</span>"
$Msg += $signature

$mail.To = 'somemail@test.test'
$mail.Subject = 'Testing Signature'
$mail.HTMLBody = $Msg
$mail.Send()

Outlook 用户签名存储在这里:

Get-ChildItem -Path "$env:APPDATA\Microsoft\Signatures"
# Results
<#
    Directory: C:\Users\SomeUserName\AppData\Roaming\Microsoft\Signatures


Mode                 LastWriteTime         Length Name                                                                                                                                              
----                 -------------         ------ ----                                                                                                                                              
d-----         24-May-21     21:53                SomeUserName - full_files                                                                                                                             
d-----         24-May-21     21:53                SomeUserName - short_files                                                                                                                            
-a----         24-May-21     21:53          43114 SomeUserName - full.htm                                                                                                                               
-a----         24-May-21     21:53          46643 SomeUserName - full.rtf                                                                                                                               
-a----         24-May-21     21:53           1468 SomeUserName - full.txt                                                                                                                               
-a----         24-May-21     21:53          41008 SomeUserName - short.htm                                                                                                                              
-a----         24-May-21     21:53          41724 SomeUserName - short.rtf                                                                                                                              
-a----         24-May-21     21:53            120 SomeUserName - short.txt
#>

因此您可以检查每个并确定在邮件正文中使用哪个。像这样说吧。

$UserSignature = Get-Content -Path ((Get-ChildItem -Path "$env:APPDATA\Microsoft\Signatures" -filter '*full.htm').FullName)
$Mail.HTMLBody = "$Msg`n`n
$UserSignature"