Thanks so much Michael. I'm just starting out and yes, I need a class or 3. And a book.
I took a new direction this morning and took some code from here: http://poshcode.org/1697 I added $att.ContentDisposition, $msg.IsBodyHTML and it seems to be working. $file = "C:\scripts\logo.jpg" $smtpServer = "smtp.server.com" $msg = new-object Net.Mail.MailMessage $att = new-object Net.Mail.Attachment($file) $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 = "[email protected]" $msg.To.Add("[email protected]") $msg.Subject = "MY SUBJECT" $msg.Attachments.Add($att) $msg.Body = { <html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml"> <head> <style type='text/css'> BottomRight { position: absolute; bottom: 2px; right: 4px; } </style> </head> <body> <font face="calibri, helvetica" size="2"> <p>Hello</p> <p>TEST</p> </font> <div id='BottomRight'> <img src='cid:logo' 'alt=logo'/> </div> </body> </html> } $smtp.Send($msg) $att.Dispose() -matt From: Michael B. Smith [mailto:[email protected]] Sent: Monday, February 28, 2011 6:30 AM To: NT System Admin Issues Subject: RE: PS send email w/ embedded image There were a number of issues here. I'm guessing that you don't really want a PowerShell class, but just a script that works. Here is a script that works. I do want to share a couple of caveats.... I'm not sure WHY you are using the messageParameters array. I left it in, but I would not use it in my code. Now, this is taking a PowerShell script and making it look an awful lot like a C# program. But if you want to display inline images, that's the way you have to go. The Send-MailMessage cmdlet will only accept filepaths as the value for the Attachment parameter, not System.Net.Mail.Attachment objects. See (gcm send-mailmessage).parameters.Attachments to verify that. Also, in PowerShell, except on COM objects you should never be calling dispose. Set the value of the variable to $null and that will decrement the reference count. If the reference count is zero, then the next time the garbage-collector runs, it will clean up the variable. If you need it to be cleaned up right now, then force the GC to run: [System.GC]::Collect() will do it in PowerShell. -----start----- $mail = new-object System.Net.Mail.MailMessage $Attachment = New-Object System.Net.Mail.Attachment( "C:\scripts\logo.jpg" ) $Attachment.ContentDisposition.Inline = $true $Attachment.ContentDisposition.DispositionType = "Inline" $Attachment.ContentType.MediaType = "image/jpg" $Attachment.ContentId = "logo" $body = @' <html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml"> <head> <style type="text/css"> BottomRight { position: absolute; bottom: 2px; right: 4px; } </style> </head> <body> <font face="calibri, helvetica" size="2"> <p>Hello</p> </font> <div id="BottomRight"> <img src="cid:logo" "alt=logo"/> </div> </body> </html> '@ if( 0 ) { $messageParameters = @{ From = "[email protected]" To = "[email protected]" SmtpServer = "win2008r2ex2010" Subject = "Yet another test email" Body = $body } } else { $messageParameters = @{ From = "[email protected]" To = "[email protected]" SmtpServer = "mxserver" Subject = "Test Email" Body = $body } } $mail.To.Add( $messageParameters.To ) $mail.From = $messageParameters.From $mail.Subject = $messageParameters.Subject $mail.Body = $messageParameters.Body $mail.IsBodyHtml = $true $mail.Attachments.Add( $attachment ) ## ## now send the email ## $smtpClient = new-object system.net.mail.smtpclient( $messageParameters.SmtpServer ) $smtpClient.Send( $mail ) $smtpClient = $null $Attachment = $null $mail = $null -----end----- Regards, Michael B. Smith Consultant and Exchange MVP http://TheEssentialExchange.com From: Matthew Bullock [mailto:[email protected]] Sent: Friday, February 25, 2011 8:48 PM To: NT System Admin Issues Subject: PS send email w/ embedded image I was hoping someone might be able to help me out with a poweshell script. The script sends a simple email, but I'm trying to embed a logo image and it's not working. All I get is an empty square for the image. Any help would be much appreciated, -matt $Attachment = New-Object System.Net.Mail.Attachment("C:\scripts\logo.jpg") $Attachment.ContentDisposition.Inline = $True $Attachment.ContentDisposition.DispositionType = "Inline" $Attachment.ContentType.MediaType = "image/jpg" $Attachment.ContentId = "logo" $body = { <html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml"> <head> <style type='text/css'> BottomRight { position: absolute; bottom: 2px; right: 4px; } </style> </head> <body> <font face="calibri, helvetica" size="2"> <p>Hello</p> </font> <div id='BottomRight'> <img src='cid:logo alt=logo'/> </div> </body> </html> } $messageParameters = @{ From = "[email protected]<mailto:[email protected]>" To = "[email protected]<mailto:[email protected]>" SmtpServer = "mxserver" Subject = "Test Email" Body = $body } Send-MailMessage @messageParameters -BodyAsHtml $Attachment.Dispose() ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected]<mailto:[email protected]> with the body: unsubscribe ntsysadmin ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected]<mailto:[email protected]> with the body: unsubscribe ntsysadmin ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected] with the body: unsubscribe ntsysadmin
