2017-06-12 78 views
-4

如何使用PowerShell將HTML文件嵌入到電子郵件正文中?這是我到目前爲止:如何在電子郵件正文中嵌入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 "[email protected]" -From "[email protected]" -SmtpServer Ymail -Credential $cred -Subject " Report:" -Body $ReportLink -BodyAsHtml 

我還是新來的PowerShell。謝謝你在前進

+0

它將是明智的,如果你分享你迄今爲止所嘗試的,以及你被擊中的地方 – Venkatakrishnan

+0

@Venkatakrishnan看到上面的代碼 – Immortal

+0

現在檢查我的答案 – Venkatakrishnan

回答

0

假設無論你想更改爲HTML格式的數據是$結果數組

$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) 
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) 
相關問題