使用Powershell带有内嵌附件的电子邮件

时间:2018-11-16 11:46:51

标签: powershell email

要求:

发送带有内嵌图像且不附加使用Powershell作为附件的图像的邮件。

我的Powershell代码:

$From = "From@gmail.com"
$ToEmails = "ToEmails@gmail.com"
$CcEmails = "CcEmails@gmail.com"
$userName = "From@gmail.com"
$ImageName = "codecover.png"
$FilePath = "D:\$($ImageName)"
$Attachment = @($FilePath)
$Subject = "Test Subject"
$password = "From Email password"
$SMTPServer = "smtp.outlook.com"
$SMTPPort = "587"
$Body += "<img src='$($ImageName)' />"

[string][ValidateNotNullOrEmpty()]$password = $password
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$Credentials = New-Object Management.Automation.PSCredential ($userName, $securePassword)

Send-MailMessage -From $From -to $ToEmails -Cc $CcEmails -Subject $Subject -BodyAsHtml -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credentials -Attachments $Attachment –DeliveryNotificationOption OnSuccess

我的结果问题:

带有内联附件的邮件发送结果,但是我不想在附件中添加相同的图片。请在下面找到屏幕截图

enter image description here

请建议我如何发送内嵌图像而不在附件中附加该图像。

1 个答案:

答案 0 :(得分:1)

您可以对其进行base64编码。

$Byte    = [system.io.file]::ReadAllBytes("C:\yourImage.png")
$Base64  = [System.Convert]::ToBase64String($Byte)
$Content = '{0}{1}{2}' -f '<img src="data:image/png;base64,',$Base64,'">'

将内容用作消息正文。