Reputation: 422
Requirement:
Send a mail with inline image with out attaching the image as attachment using powershell.
My Powershell Code:
$From = "[email protected]"
$ToEmails = "[email protected]"
$CcEmails = "[email protected]"
$userName = "[email protected]"
$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
Problem with my Result:
Result of the mail sending with inline attachment but the same image is in attachments which i dont want. Please find the screen shot below
Please suggest me how to send the inline images without attaching that image in attachments.
Upvotes: 1
Views: 2680
Reputation: 3063
You can have it base64 encoded.
$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,'">'
Use the content as the message Body.
Upvotes: 3