Send inline images using send-mailmessage

为君一笑 提交于 2019-12-04 02:20:12

问题


I am trying to send an html formatted email with inline images from the command line using send-mailmessage. I have looked everywhere and wasn't able to find the solution. I am able to get the email to send but the recipient doesn't receive the images. Here is my code:

$smtp = "smtp.server.com" 
$to = "recp@example.com" 
$from = "from@example.com" 
$subject = "This is a subject"
$body = (-join(Get-content \email.html))

send-MailMessage -SmtpServer $smtp -To $to -From $from  -Subject $subject -BodyAsHtml  -Body $body

If there is a problem in the code or if there is a different solution please let me know. Thank you!


回答1:


You can't do that with Send-MailMessage cmdlet. To include inline image in message you have to write your own function using .NET's Net.Mail.SmtpClient class.

Alternatively, you can just grab David Wyatt's Send-MailMessage function, which already has all you need:

The Send-MailMessage cmdlet exposes most of the basic functionality for sending email, but you don't get a lot of control over things like attachments or alternate views. Someone on the TechNet forums was asking how to embed images into an HTML mail message, and I decided to write a version of Send-MailMessage that supports this. I started with a proxy function for the Send-MailMessage cmdlet, so all of the parameters and usage should be intact. The main difference is that I added an -InlineAttachments argument, which accepts a hashtable of pairs in the format 'ContentId'='FilePath'. You can then embed the resources into an HTML body by using URLs of the format cid:ContentId.



来源:https://stackoverflow.com/questions/33041698/send-inline-images-using-send-mailmessage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!