使用发送邮件消息发送内嵌图像 [英] Send inline images using send-mailmessage

查看:103
本文介绍了使用发送邮件消息发送内嵌图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

推荐答案

使用Send-MailMessage cmdlet无法做到这一点.要在邮件中包含嵌入式图像,您必须使用.NET的

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.

或者,您可以获取David Wyatt的发送MailMessage 函数,它已经满足您的所有需求:

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

Send-MailMessage cmdlet公开了大多数基本功能 发送电子邮件,但您对诸如此类的东西没有太多控制权 附件或备用视图.在TechNet论坛上有人 在询问如何将图像嵌入HTML邮件中时,我决定 编写支持该功能的Send-MailMessage版本.我已开始 使用Send-MailMessage cmdlet的代理功能,因此所有 参数和用法应完整无缺.主要区别是我 添加了一个-InlineAttachments参数,该参数接受一个哈希表 以'ContentId'='FilePath'格式配对.然后,您可以嵌入 通过使用以下格式的URL将资源转换成HTML主体 cid:ContentId.

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.

这篇关于使用发送邮件消息发送内嵌图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆