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



我想在PowerShell中为电子邮件生成嵌入式图像。但运气不好。我使用<p><img src='file:///C:tempPicture1.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:tempPicture1.png' /></p>



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

谢谢,

不幸的是,直接引用C:上的文件将不起作用(有太多的安全问题),Send-MailMessage不会拾取它。相反,你有两个选择,而且,听起来令人困惑和琐碎,这在很大程度上取决于你的目标电子邮件程序。

CID图像嵌入

"老派"方法是将图像附加到电子邮件中,并使用内容id (CID)引用将两者链接起来。如果您使用的是桌面Outlook应用程序,这是首选的方法。缺点是人们并不总是信任带有附件的电子邮件,并且发送带有附件的电子邮件会产生垃圾邮件/过滤影响,所以有时不总是首选。此外,一些基于web的电子邮件提供商在正确显示图像方面存在问题。

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

内联嵌入将数据嵌入为base64编码数据(基本上将二进制图像数据更改为纯文本),并由电子邮件客户端/浏览器解码。这意味着没有附件,通常交付更成功。因为base64编码在网络中使用,所有基于网络的电子邮件客户端都不会有显示它们的问题。缺点是桌面版的Outlook 2010+会显示"默认屏蔽的图像"。

$file = Get-Content 'C:tempPicture1.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

最新更新