Wednesday, 20 August 2014

Powershell - send email with attached file


$date = Get-Date
$file = "C:\temp\report.html"

$smtpServer = "mai.yourdomain.com"
$att = new-object Net.Mail.Attachment($file)
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "sender@yourdomain.com"
$msg.To.Add("receiver.yourdomain.com")
$msg.Subject = "report from " + $date
$msg.Body = "see attachment"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()



$outfile = “c:\temp\sent.txt”

“Email Sent at ” + $date | out-file $outfile

No comments:

Post a Comment