如何使用 MailMessage 在 powershell 电子邮件中嵌入图像

How to embed Images in a powershell email using MailMessage

我有一封来自 PS 的电子邮件。我一直在尝试做的是在电子邮件中包含嵌入的图像(不是附件)。以下是我目前所拥有的:

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

在上面,我在 Body.html 文件中添加了图像标签。如果我直接打开 html,它看起来像预期的那样正确显示图像。

然而,当我发送邮件时,图像只显示为带边框的白框。似乎脚本没有加载文件中的图像。

有没有人做过类似的,有什么建议吗?

您必须将图像添加为常规附件。 HTML 正文必须通过 cid 属性引用这些附件:<img src="cid:xyz"> 其中 "xyz" 是附件 MIME 部分的 Content-ID MIME 属性的值。