HTML embedded image in the email not displayed

前提是你 提交于 2020-01-07 06:13:49

问题


This is the first time I am writing power shell scripting, I am trying to automatically send email reports to our users. I am creating outlook object and trying to use it.

$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:\MyImages\Volume.png'/><br/><br/><hr><br/><img src='C:\MyImages\Value.png'/><br/><br/><hr><br/><hr>

$FinalText = $Text+$MessageImages

Now, I am creating the outlook object and sending 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.Send()

When I run this script, All images in $MessageImages are visible in the email in my computer as the images are present in the location mentioned, but when I send it to someone else, it is showing x mark. I can understand that, it is not able to find the image and so not displaying the embedded image. Can someone help me in solving this?

Edit : This is not related to System.Net.Mail.MailMessage or Send-Mailmessage. This is using Outlook object - New-Object -com Outlook.Application and so it cannot be considered as duplicate.


回答1:


If u need to send the HTML report with images in email, u have to attach the images with that email. Refer the below one,

$files = Get-ChildItem C:\MyImages\             
Foreach($file in $files)                   
{                     
$filename = $file.FullName                    
$attachment = New-Object System.Net.Mail.Attachment –ArgumentList  
$filename.ToString()         
$attachment.ContentDisposition.Inline = $True            
$attachment.ContentDisposition.DispositionType = "Inline"         
$attachment.ContentType.MediaType = "image/jpg"             
$attachment.ContentId = $file.ToString()          
$emailMessage.Attachments.Add($attachment)         
}         
$SMTPClient.Send($emailMessage)        
$attachment.Dispose();           
$emailMessage.Dispose();         

In the HTML mention the image source as img src="cid:Volume.png" and img src='cid:Value.png'

Try it'll works



来源:https://stackoverflow.com/questions/32469812/html-embedded-image-in-the-email-not-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!