gpt4 book ai didi

powershell - Powershell-使用.htm文件作为电子邮件正文

转载 作者:行者123 更新时间:2023-12-03 00:24:41 25 4
gpt4 key购买 nike

我正在尝试编写一个Powershell脚本,该脚本将由计划任务启动,该脚本将返回我们数据库备份的详细信息,将其保存到.htm文件中并通过电子邮件发送结果。

到目前为止,获取备份信息并将其保存到.htm文件是可以的,但是我无法弄清楚如何使电子邮件正文看起来像.htm文件。此刻的电子邮件正文看起来像原始的html代码。

至今:

$a = "<style>"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 10px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 10px;border-style: solid;border-color: black;}"
$a = $a + "</style>"

$date = ( get-date ).ToString('yyyyMMdd')





[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "LOCALHOST\SQLX64"

$dbs=$s.Databases

#Retrieves the last backup dates - both for FULL and LOG backups

$backups = $dbs | SELECT Name,LastBackupDate, LastLogBackupDate | ConvertTo-HTML -head $a -body "<H2>Database Backup Details $date </H2>" | Out-File $("D:\SQL Backup Log Script\Logs\"+ $date +"_DB01 Backup Log.htm")


$EmailFrom = "IT@##.net"
$emailto = "##@##.net"
$Subject = "DB01 SQL Backup Log"
$Body = Get-Content ("D:\SQL Backup Log Script\Logs\"+ $date +"_Backup Log.htm")
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("##", "##");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

谢谢,
夏洛特。

最佳答案

尝试在send()方法之前添加以下行:

$SMTPClient.isbodyhtml= $true

评论后编辑:

您必须使用此对象:

$ SMTPClient =新对象System.Net.Mail.MailMessage

关于powershell - Powershell-使用.htm文件作为电子邮件正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9112549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com