如何在电子邮件正文中嵌入HTML文件

时间:2017-06-12 12:33:49

标签: powershell

如何使用powershell在电子邮件正文中嵌入HTML文件?这就是我到目前为止所做的:

 ##Connect to the data source using the connection details and T-SQL command we provided above, and open the connection
        $connection = New-Object System.Data.OleDb.OleDbConnection $connectionDetails
        $command = New-Object System.Data.OleDb.OleDbCommand $sqlCommand,$connection
        $connection.Open()
        ##Get the results of our command into a DataSet object, and close the connection
        $dataAdapter = New-Object System.Data.OleDb.OleDbDataAdapter $command
        $dataSet = New-Object System.Data.DataSet
        $dataAdapter.Fill($dataSet)
        $connection.Close()

        $body = $TableHeader
        $dataSet.Tables | Select-Object -Expand Rows  |
        Select-Object * -ExcludeProperty Comment, RowError, RowState, Table, ItemArray, HasErrors |
        ConvertTo-HTML -head $a –body $body |
        Out-File $OutputFile

        $ReportLink = "file://serverr/c$/Output/Report.Html" 


        Write-Output " Reporting"
        $MailUsername = "you"
        $MailPassword = "your pasword"
        $cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($MailUsername,(ConvertTo-SecureString -String $MailPassword -AsPlainText -Force))
        Send-MailMessage -To "abc@ymail.com" -From "xyz@ymail.com" -SmtpServer Ymail -Credential $cred -Subject " Report:" -Body $ReportLink -BodyAsHtml

我仍然是PowerShell的新手。提前谢谢

2 个答案:

答案 0 :(得分:0)

假设您想要更改为HTML格式的数据位于$ Result Array

$EmailFrom = ""
$EmailTo =   ""
$SMTPServer = "" 
$EmailSubject ="" 

      $Report = "<HTML><TITLE>Title</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Heading </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>Row1</B></TD><TD><B>Row2</B></TD><TD><B>Row3</B></TD><TD><B>Row4</B></TD></TR>"
    Foreach($Entry in $Result) 
    {  
    $Report += "<TR>"
    $Report += "<TD>$($Entry.Value1)</TD><TD>$($Entry.Value2)</TD><TD>$($Entry.Value3)</TD><TD align=center>$($Entry.Value4)</TD></TR>" 
    } 
    $Report += "</Table></BODY></HTML>" 

$mailmessage = New-Object system.net.mail.mailmessage  
$mailmessage.from = ($EmailFrom)  
$mailmessage.To.add($EmailTo) 
$mailmessage.Subject = $EmailSubject 
$mailmessage.Body = $Report 
$mailmessage.IsBodyHTML = $true 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer)   
$SMTPClient.Send($mailmessage)

答案 1 :(得分:0)

我需要添加这个脚本,它就像魔术一样

$message = new-object System.Net.Mail.MailMessage
        $message.From = $from
        $message.To.Add($to)
        $message.CC.Add($CC)
        $Subject = " put subject here"
        $message.IsBodyHtml = $True
        $message.Subject = $Subject
        $attach = new-object Net.Mail.Attachment($attachment)
        $message.Attachments.Add($attach)
        $message.body = $body
        $smtp = new-object Net.Mail.SmtpClient($smtpserver)
        $smtp.Send($message)