ds_user
ds_user

Reputation: 99

Embed image in email using outlook object in powershell

I already posted a question related to this (HTML embedded image in the email not displayed ) and updated my script to include cid, but now my images are attached rather than coming inline. I am not sure how I can use inline images in outlook object.

$Text --> "<br /><font face='Arial'><b><i>For Full Interactive Viewing <a href=http://www.google.com>Click Here</a></i></b></font><br/>"

$MessageImages --> <br/><img src='cid:Volume.png'/><br/><br/><hr><br/><img src='C:\MyImages\Value.png'/><br/><br/><hr><br/><hr>

$FinalText = $Text+$MessageImages

Then I am creating an outlook object and sending it with attachments in it.

$o = New-Object -com Outlook.Application

$mail = $o.CreateItem(0)

$mail.importance = 2

$mail.subject = “Reports - Automated Email from user list “

$mail.HTMLBody = $FinalText

$mail.SentOnBehalfOfName ="*********"

$mail.To = "*******"

$mail.Attachments.Add($file.ToString())

$mail.Send()

It is attached without any issue, but not embedded inline. Can anyone help on this.

I dont have smtp server details and so I cannot use smtp client. So I tried to use this class System.Net.Mail.Attachment as below, but when I tried to add the attachment as below,

PS H:\> $file


    Directory: C:\Users\Images


Mode                LastWriteTime     Length Name                                                                                                                                                                                                                  
----                -------------     ------ ----                                                                                                                                                                                                                  
-a---       23/09/2015  4:04 p.m.     132361 Volume.png   


 $attachment = New-Object System.Net.Mail.Attachment –ArgumentList ($file.FullName).ToString() 
 $attachment.ContentDisposition.Inline = $True
 $attachment.ContentDisposition.DispositionType = "Inline"
 $attachment.ContentType.MediaType = "image/png"
 $attachment.ContentId = $file.Name.ToString()
 $mail.Attachments.Add($attachment)

I am getting this error -

Exception calling "Add" with "1" argument(s): "Value does not fall within the expected range."
At C:\work\Automated.ps1:130 char:23
+  $mail.Attachments.Add <<<< ($attachment)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

Can someone assist me on how to use inline images in outlook object.?

Upvotes: 0

Views: 3503

Answers (2)

Kaushik
Kaushik

Reputation: 111

Updated and Complete Answer incase if anyone needs :

$Text = "<br /><font face='Arial'><b><i>For Full Interactive Viewing <a 
href=http://www.google.com>Click Here</a></i></b></font><br/>"

$MessageImages = "<br/><img src='C:\Users\E5559405\Desktop\IT\Image.jpg'/><br/>"

$FinalText = $Text+$MessageImages

$o = New-Object -ComObject Outlook.Application

$mail = $o.CreateItem(0)

$mail.importance = 2

$mail.subject = “Reports - Automated Email from user list “

$mail.HTMLBody = $FinalText

$mail.To = "[email protected]"

$mail.Send()

Upvotes: 0

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66245

You will need to set the PR_ATTACH_CONTENT_ID property to the matching value of the cid attribute (Volume.png in your case):

$attach = $mail.Attachments.Add($attachment)
$attach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "Volume.png")

Upvotes: 1

Related Questions