问题描述
我有一封来自PS的电子邮件。我一直在试图做的是在电子邮件中嵌入图像(不包括附件)。以下是我到目前为止:
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)
}
在上面,我添加了图像标签到Body.html文件。如果我直接打开html,它看起来像预期的图像显示正确。
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?
推荐答案
您必须将图片添加为常规附件。然后,HTML主体必须通过cid属性引用这些附件:< img src =cid:xyz>
其中xyz是Content-ID的值附件MIME部分的MIME属性。
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.
这篇关于如何使用MailMessage将图像嵌入PowerShell电子邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!