附加到powershell脚本中的电子邮件

时间:2012-06-19 17:12:39

标签: powershell

我有以下powershell脚本:

$csv = Import-Csv -Path D:\temp\Audit.csv | Where-Object {$_.PrivateLabelSeqId -eq "602"} | Measure-Object
$Fun = $csv.Count
$mailBody = 
@"
There are <b> $Fun </b> available!
"@

Send-MailMessage -Body $mailBody -BodyAsHtml `
-From "mail@mail.com" -To "mailto@mail.com" `
-Subject "Audit - FUN" -Encoding $([System.Text.Encoding]::UTF8) `
-Attachment "D:\temp\Audit.csv" `
-SmtpServer "192.0.0.20"

但我没有收到任何电子邮件。然而,如果没有附件,它似乎工作得很好。关于如何解决这个问题的任何想法? 感谢

1 个答案:

答案 0 :(得分:0)

我曾遇到过同样的问题,我通过使用System.Net.Mail来解决它。

$filenameAndPath = (Resolve-Path .\$file).ToString()
$from = 'mail@mail.com'
$to = "mailto@mail.com" `
$Subject "Audit - FUN" -Encoding $([System.Text.Encoding]::UTF8) 
[void][Reflection.Assembly]::LoadWithPartialName('System.Net') | out-null

$message = New-Object System.Net.Mail.MailMessage($from, $to, $subject, $subject)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath, 'text/plain')
$message.Attachments.Add($attachment)
$smtpClient = New-Object System.Net.Mail.SmtpClient
$smtpClient.host = '192.0.0.20'
$smtpClient.Send($message)

阅读有关System.Net.Mail.Attachement类的更多信息 http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx