powershell - 如何发送包含从excel读取的内容的Outlook邮件(csv)

时间:2017-07-11 18:09:19

标签: excel powershell email

任何人都可以举例说明如何发送包含从excel(csv)读取的内容的Outlook邮件吗?

它似乎适用于txt但不适用于excel

$o = New-Object -com Outlook.Application
$mail = $o.CreateItem(0)
$yesterday = (get-date).AddDays(-1).ToString('MMM dd, yyyy')

$mail.subject = "Missing/Empty Files"

$mail.Attachments.Add("I:\BadFileReport_20170711_0103.csv")

$mail.HTMLBody += "<span style='font-family:Courier New;font-size:12pt'>"  

$mail.HTMLbody = $mail.HTMLBody + "<B>Please check missing/empty files for yesterday</B><br><br>" 

$file1 = import-csv I:\BadFileReport_20170711_0103.csv


foreach ($line in $file1)
{
    $mail.HTMLbody = $mail.HTMLbody  + $line   
}

$mail.HTMLbody = $mail.HTMLbody + $title2 + "<br>"




$signature = "<br>Regards,<br>"

$mail.HTMLbody = $mail.HTMLbody + $signature



$mail.To = "12345@imcnam.ssmb.com"

write-host "mail sent"

$mail.save()

输出如下:

enter image description here

1 个答案:

答案 0 :(得分:1)

您导入CSV文件,现在只需使用ConvertTo-HTML cmdlet使Outlook友好。

$o = New-Object -com Outlook.Application
$mail = $o.CreateItem(0)
$yesterday = (get-date).AddDays(-1).ToString('MMM dd, yyyy')

$mail.subject = "Missing/Empty Files"

$mail.Attachments.Add("I:\BadFileReport_20170711_0103.csv")

$mail.HTMLBody += "<span style='font-family:Courier New;font-size:12pt'>"  

$mail.HTMLbody = $mail.HTMLBody + "<B>Please check missing/empty files for yesterday</B><br><br>" 

$mail.HTMLbody = $mail.HTMLBody + = import-csv I:\BadFileReport_20170711_0103.csv | ConvertTo-HTML -as Table -Property * -Fragment

$signature = "<br>Regards,<br>"

$mail.HTMLbody = $mail.HTMLbody + $signature

$mail.To = "12345@imcnam.ssmb.com"

write-host "mail sent"

$mail.save()