当前位置:网站首页>Powershell 使用.Net对象发送邮件
Powershell 使用.Net对象发送邮件
2020-11-08 11:26:00 【osc_3re0wjem】
发送邮件的方式有多种, 个人习惯使用windows powershell 自带的Send-MailMessage 可以实现发送邮件, 这次使用.Net来发送邮件,而且需要插入本地图片到HTML文件当中, 需要注意的是获取的图片name 需要与HTML中的cid:name一致, 参考代码如下:
$EmailAddress = '[email protected]'
$subject = 'Test Use Net Send Mail'
$SmtpServer = "mail.contoso.com"
$htmlbody = @'
<body>
<div>
<img src="cid:telphone.jpg" style="display:inline-block">
</div>
<span>This is test mail, use .NET send mail</span>
<div>
<img src="cid:home.png" style="display:inline-block">
</div>
</body>
'@
$MailMessage = New-Object System.Net.Mail.Mailmessage
$imagepath = 'D:\script\images'
$files = Get-ChildItem $imagepath
foreach ($file in $files)
{
$Attachment = New-Object Net.Mail.Attachment("$imagepath\$file")
$Attachment.ContentDisposition.Inline = $True
$Attachment.ContentDisposition.DispositionType = "Inline"
$Attachment.ContentType.MediaType = "image/png"
$Attachment.ContentId = $file.ToString() # file name must be equal inert into html image cid: name
$MailMessage.Attachments.Add($Attachment)
}
$MailMessage.To.Add($EmailAddress)
$MailMessage.from = '[email protected]'
$MailMessage.Subject = $subject
$MailMessage.Body = $htmlbody
$MailMessage.IsBodyHTML = $true
$MailMessage.BodyEncoding = [System.Text.Encoding]::UTF8
$MailMessage.Priority = "High"
$SmtpClient = New-Object Net.Mail.SmtpClient($SmtpServer)
$SmtpClient.UseDefaultCredentials = $false
#$SmtpClient.Credentials = New-Object System.Net.NetworkCredential("[email protected]", "123456")
$SmtpClient.Send($MailMessage)
$Attachment.dispose()
版权声明
本文为[osc_3re0wjem]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4356468/blog/4708042
边栏推荐
- 阿里教你深入浅出玩转物联网平台!(附网盘链接)
- python小工具:编码转换
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- TCP协议如何确保可靠传输
- Japan PSE certification
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- 攻防世界之web新手题
- 最全!阿里巴巴经济体云原生实践!(附网盘链接)
- 比Python快20%,就问你兴不兴奋?
- Test requirements for MIC certification of Bluetooth 2.4G products in Japan
猜你喜欢
11 server monitoring tools commonly used by operation and maintenance personnel
PX4添加新的应用
Second assignment
Bohai bank million level fines continue: Li Volta said that the governance is perfect, the growth rate is declining
Adobe media encoder / me 2021 software installation package (with installation tutorial)
“1024”征文活动结果新鲜出炉!快来看看是否榜上有名?~~
个人目前技术栈
Is software testing training class easy to find a job
Solve Safari browser download file name garbled problem
print( 'Hello,NumPy!' )
随机推荐
游戏优化性能杂谈(十一) - 知乎
How to deploy pytorch lightning model to production
YGC troubleshooting, let me rise again!
PMP考试通过心得分享
Dogs can also operate drones! You're right, but it's actually an autonomous drone - you know
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)
Entry level! Teach you how to develop small programs without asking for help (with internet disk link)
python小工具:编码转换
墨者学院SQL注入解题
学习小结(关于深度学习、视觉和学习体会)
Tidb performance competition 11.02-11.06
值得一看!EMR弹性低成本离线大数据分析最佳实践(附网盘链接)
在51CTO学院Get到PMP证书
Solve Safari browser download file name garbled problem
C语言I博客作业03
Hematemesis! Alibaba Android Development Manual! (Internet disk link attached)
函数周期表丨筛选丨值丨SELECTEDVALUE - 知乎
架构师(2020年11月)
next.js实现服务端缓存