PowerShell 中电子邮件的嵌入式图像

Embedded Images for an email in PowerShell

我想在 PowerShell 中为电子邮件生成嵌入式图像。但没有运气。我正在使用 <p><img src='file:///C:\temp\Picture1.png' /></p>

    # Email Subject Set Here
    $subject="Your password will expire soon"
  
    # Email Body Set Here, Note You can use HTML, including İmages.
    $body ="

<P style='font-size: 12pt; font-family: Arial Narrow;'>Dear FIRST_NAME,</P>
        <P style='font-size: 12pt; font-family: Arial Narrow;'>The current password of the account 'PRINCIPAL_ACCOUNT_NAME' in the 'FQDN_DOMAIN' AD domain will expire at <B>PWD_EXPIRY_DATE</B>. Please change your password as soon as possible!</P>
        <P style='font-size: 12pt; font-family: Arial Narrow;'><U>The new password must meet the following requirements:</U></P>
        <UL>
            <LI><P style='font-size: 12pt; font-family: Arial Narrow;'>Min. nr. of characters in a PWD = 'PWD_MIN_LENGTH characters' <BR><I>(The minimum number of characters to be used in a password)</I></P></LI>
            <LI><P style='font-size: 12pt; font-family: Arial Narrow;'>Min. PWD age = 'PWD_MIN_AGE days' <BR><I>(Minimum number of days the new password must be used before it can be changed again)</I></P></LI>
            <LI><P style='font-size: 12pt; font-family: Arial Narrow;'>Max. PWD age = 'PWD_MAX_AGE days' <BR><I>(Maximum number of days the new password can be used before it must be changed again)</I></P></LI>
            <LI><P style='font-size: 12pt; font-family: Arial Narrow;'>Nr. of previous PWDs in history = 'PWD_HISTORY' <BR><I>(Number of new and unique passwords required before an old password can be reused again)</I></P></LI>
            <LI><P style='font-size: 12pt; font-family: Arial Narrow;'>Password complexity enabled? = 'PWD_COMPLEX' <BR><I>(The new password must meet complexity requirements ONLY when configured to 'TRUE')</I></P></LI>
        </UL>
        
        
        <p><img src='file:///C:\temp\Picture1.png' /></p>




    
"
Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High -Encoding $encoding

谢谢,

不幸的是,直接引用 C:\ 上的文件是行不通的(这有太多安全问题)并且 Send-MailMessage 不会选择它。相反,您有 2 个选项,而且,尽管听起来令人困惑和微不足道,但这在很大程度上取决于您的目标电子邮件程序。

CID 图像嵌入

“老派”方法,是将图像附加到电子邮件中并使用 Content-ID (CID) 引用 link 两者。如果您使用 desktop Outlook 应用程序,这是首选方法。缺点是人们并不总是信任带附件的电子邮件,发送带附件的电子邮件会产生 spam/filtering 影响,因此有时并不总是首选。此外,一些基于 Web 的电子邮件提供商无法正确显示图像。

$body = "<p>.... <img src='cid:Picture1.png' alt='Embedded Image'>   ....</p>"
$file = 'C:\temp\Picture1.png'
Send-Mailmessage -Attachments $File -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High -Encoding $encoding

内联嵌入

内联嵌入将数据嵌入为 base64 编码数据(基本上将二进制图像数据更改为纯文本),电子邮件 client/browser 对其进行解码。这意味着没有附件,通常交付成功率更高。由于 web 中使用 base64 编码,因此所有基于 web 的电子邮件客户端都可以毫无问题地显示它们。缺点是桌面版Outlook 2010+会显示“图片默认被屏蔽”

$file = Get-Content 'C:\temp\Picture1.png'
$Bytes = [System.Text.Encoding]::Unicode.GetBytes($file)
$EncodedText =[Convert]::ToBase64String($Bytes)

$body = "<p>.... <img src='data:image/png;base64,$EncodedText' alt='Embedded Image'>   ....</p>"

Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High -Encoding $encoding