使用 send-mailmessage 发送内联图像

Send inline images using send-mailmessage

我正在尝试使用 send-mailmessage 从命令行发送带有内联图像的 html 格式的电子邮件。我到处都看过,但找不到解决方案。我能够收到要发送的电子邮件,但收件人没有收到图像。这是我的代码:

$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

如果代码中有问题或者有不同的解决方案,请告诉我。谢谢!

您不能使用 Send-MailMessage cmdlet 执行此操作。要在消息中包含内联图像,您必须使用 .NET Net.Mail.SmtpClient class.

编写自己的函数

或者,您可以只获取 David Wyatt 的 Send-MailMessage 函数,它已经拥有您需要的一切:

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.