当前位置:网站首页>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
边栏推荐
- Understanding design patterns
- PMP考试通过心得分享
- 来自朋友最近阿里、腾讯、美团等P7级Python开发岗位面试题
- 笔试面试题目:盛水最多的容器
- How to deploy pytorch lightning model to production
- How does spotify drive data-driven decision making?
- Rust : 性能测试criterion库
- 为什么 Schnorr 签名被誉为比特币 Segwit 后的最大技术更新
- 还不快看!对于阿里云云原生数据湖体系全解读!(附网盘链接)
- Automatically generate RSS feeds for docsify
猜你喜欢
Rust:命令行参数与环境变量操作
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
个人目前技术栈
Introduction to mongodb foundation of distributed document storage database
阿里出品!视觉计算开发者系列手册(附网盘链接)
“1024”征文活动结果新鲜出炉!快来看看是否榜上有名?~~
蘑菇街电商交易平台服务架构及改造优化历程(含PPT)
Rust : 性能测试criterion库
Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
【计算机网络】学习笔记,第三篇:数据链路层(谢希仁版)
随机推荐
Tidb performance competition 11.02-11.06
Flink's sink: a preliminary study
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
Understanding design patterns
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
C language I blog assignment 03
运维人员常用到的 11 款服务器监控工具
211考研失败后,熬夜了两个月拿下字节offer!【面经分享】
413【毕设课设】基于51单片机无线zigbee无线智能家居光照温湿度传输监测系统
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
PDMS cutting software
【计算机网络】学习笔记,第三篇:数据链路层(谢希仁版)
你的云服务器可以用来做什么?云服务器有什么用途?
攻防世界之web新手题
When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
Function periodic table filter value selectedvalue
漫画|讲解一下如何写简历&项目
Oops, the system is under attack again
在51CTO学院Get到PMP证书
Second assignment