How to embed Images in a powershell email using MailMessage

我只是一个虾纸丫 提交于 2019-12-02 23:08:22

问题


I have an email that works from PS. What I have been trying to do is include images embedded in the email (not attachments). Below is what I have so far:

function Email
        {   
            $smtpServer = {smtp server}
            $smtpFrom = {email from}
            $smtpTo = {email to}
            $messageSubject = "test"

            $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
            $message.Subject = $messageSubject
            $message.IsBodyHTML = $true
            $credentials=new-object system.net.networkcredential({smtpUsername},{smtpPassword})

             # $message.Body = Get-Content "D:\Program Files\CymbaTech_FBNC_AM\CTDataLoader\data\TestBody.html"

        # The line below will add any attachments you have such as log files.
        $message.Attachments.Add("{path}\Image1.png")
        $message.Body = '<img src="cid:xyz">'

            $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
            $smtp.credentials=$credentials.getcredential($smtpserver,"25","basic")
            $smtp.Send($message)
        } 

In the above, I have added image tags to the Body.html file. If I open the html directly, it looks as expected with images showing correctly.

When I send the mail however, the images are just displayed as white boxes with a border. Seems that the script is not loading the images in the file.

Has anyone done similar before and have any suggestions?


回答1:


You must add images as regular attachments. The HTML body must then reference these attachments though the cid attribute: <img src="cid:xyz"> where "xyz" is the value of the Content-ID MIME attribute on the attachment MIME part.



来源:https://stackoverflow.com/questions/29237687/how-to-embed-images-in-a-powershell-email-using-mailmessage

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