Thursday, October 20, 2011

Sending mail with image in PowerShell

$smtpServer = "<smtp server name>"
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment("<image path>")
$att.ContentDisposition.Inline = $True
$att.ContentDisposition.DispositionType = "Inline"
$att.ContentType.MediaType = "image/jpeg"
$att.ContentId = "logo"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.IsBodyHTML = $True
$msg.From = "<from address>"
$msg.To.Add("<to address>")
$msg.cc.Add("<cc address>")
$msg.Subject = "<mail subject>"
$msg.Attachments.Add($att)
$msg.Body = "<mail body>... <img src='cid:logo'>"
Add-Content -Path "d:\mylog.txt" -Value "Mail Sent..."
$smtp.Send($msg)
$att.Dispose()

No comments:

Post a Comment