0

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.?

3
  • Why not use Send-Mailmessage instead of messing with a COM API? Commented Sep 23, 2015 at 5:25
  • Is it possible to use Send-Mailmessage without smtp server details? I am using local outlook appllication since I dont have smtp server address or name. Commented Sep 23, 2015 at 5:37
  • do you use attachment or inline (cid) img src="cid:Volume.png" and img src='cid:Value.png' ? which is your HTML Body content ? Commented Jun 23, 2016 at 12:37

2 Answers 2

1

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")
Sign up to request clarification or add additional context in comments.

3 Comments

Do I need to use System.Net.Mail.Attachment to specify the Inline properties or I can directly use PR_ATTACH_CONTENT_ID ?
I mean which method to use? This - $mail.Attachments.Add($file.ToString()) or System.Net.Mail.Attachment class ?
You can use either method, whatever works for you best. My example was for the Outlook Object Model method. If you are using straight MIME with SAMTP, make sure the attachment MIME part has the matching Content-ID MIME header.
0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.