Wednesday, June 25, 2014

Everyday Powershell - Part 24 - Send Email With Inline Image

Ever wanted to include inline images with an email you are sending out from a script?

We came across the requirement during our Exchange 2013 Migration.

This was ridiculously easy to achieve in Powershell 4.
Send-MailMessage -to -From -SmtpServer somemailserver -Subject "Test" -BodyAsHtml "TEST <br /><img src="attachement.png" />" -Attachments "C:\Scripts\attachement.png"

This is how we used to do it!
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
$smtpServer = "somemailserver"
$att = new-object Net.Mail.Attachment("c:\scripts\attachment.png")
$att.ContentId = "att"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$body = "Some Text<br />
<img src="cid:att" />"

$msg.From = ""
$msg.Subject = "Some Subject" + (get-date -format yyyyMMdd)
$msg.Body = $body
$msg.IsBodyHTML = $true

UPDATE - The HTML included in the script bugged out... Will fix shortly.

UPDATE 2 - reader sivaramsharma asked if send-mailmessage could handle multiple attachments, if this interests you too head over to Part 31 where we give a useful example.


  1. Hi Ben,
    Your Script works like charm for sending Single PNG files in Body of E-mail, I have several PNG files generated from several loops in single Script, Could you please provide Script for sending Multiple PNG files as in Body of E-mail for same. All PNG files will be generated and stored in c:\Reports\ in my Script.
    Thanks in Advance.

    Thanks & Regards,

  2. Hey mate thanks for the question... Attachments parameter accepts multiple values as input. Will post the exact syntax as a new post.

  3. The send mailmessage code above does not work in version powershell 4

  4. I'm sure it was just a typo...

    To fix the -BodyAsHtml just remove the double quotations around the image location and use single quotations instead.

    Thank Ben for your excellent and concise example!

    1. Thanks! I was stuck on this and that worked perfectly.

  5. How can I add a local image to this email script works perfectly fine but now I'd like to embed an image (off my c: drive) directly to the email.....everything I try is not working. I keep getting errors....

    $smtp = New-Object Net.Mail.SmtpClient("")
    $smtp.Send("","","You've been warned",


  6. I was looking for a way to send an inline image using Send-MailMessage option with no luck for days until I found your post, thanks mate, is as easy as I thought it should be.

  7. Hi Team,

    I am actually trying to put the image as first and then using the excel output where the image is not coming now.Help me

  8. This comment has been removed by the author.

  9. Hi Team,

    I am actually trying to put the image as first and then using the excel output where the image is not coming now.Help me

  10. I was doing some testing and found you can do it just fine using the send-mailmessage service just fine. Attach the files normally using this service and then in the body of the email add an image with the source of "cid:" and the filename of the file you attached. i.e. file attached is "c:\temp\test.png" would be "cid:test.png".