Thursday, October 20, 2011

Sending mail with attachment in PowerShell

$file = "c:\myfile.text"
$smtpServer = "<smtpservername>"
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($file)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "<from address>"
$msg.To.Add("<to address>")
$msg.Subject = "<mail subject>"
$msg.Body = "<mail body>"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()

No comments:

Post a Comment