We came across the requirement during our Exchange 2013 Migration.
This was ridiculously easy to achieve in Powershell 4.
Send-MailMessage -to someuser@someserver.com -From someotheruser@someserver.com -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 = "someuser@manly.nsw.gov.au" $msg.To.Add("someotheruser@manly.nsw.gov.au") $msg.Subject = "Some Subject" + (get-date -format yyyyMMdd) $msg.Body = $body $msg.IsBodyHTML = $true $msg.Attachments.Add($att) $smtp.Send($msg) $att.Dispose() |
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.
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.
Hi Ben,
ReplyDeleteYour 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,
Siva
Hey mate thanks for the question... Attachments parameter accepts multiple values as input. Will post the exact syntax as a new post.
ReplyDeleteThe send mailmessage code above does not work in version powershell 4
ReplyDeleteI'm sure it was just a typo...
ReplyDeleteTo 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!
Thanks! I was stuck on this and that worked perfectly.
DeleteHow can I add a local image to this email script below....it 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....
ReplyDelete$smtp = New-Object Net.Mail.SmtpClient("server.123.com")
$smtp.Send("XYZ@123.com","ABC@123.com","You've been warned",
"Warning...
")
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.
ReplyDeleteHi Team,
ReplyDeleteI am actually trying to put the image as first and then using the excel output where the image is not coming now.Help me
This comment has been removed by the author.
ReplyDeleteHi Team,
ReplyDeleteI am actually trying to put the image as first and then using the excel output where the image is not coming now.Help me
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".
ReplyDeleteGreat and I have a super provide: Full House Reno home addition contractor
ReplyDelete