当前位置:网站首页>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
边栏推荐
- PCIe enumeration process
- 墨者学院SQL注入解题
- Adobe media encoder / me 2021 software installation package (with installation tutorial)
- 狗狗也能操作无人机!你没看错,不过这其实是架自动驾驶无人机 - 知乎
- 【计算机网络】学习笔记,第三篇:数据链路层(谢希仁版)
- 你搞不懂与别人的差距,永远成不了架构师!月薪15K和月薪65K,你差在那了?
- Bohai bank million level fines continue: Li Volta said that the governance is perfect, the growth rate is declining
- next.js实现服务端缓存
- Adobe media encoder /Me 2021软件安装包(附安装教程)
- How did Julia become popular?
猜你喜欢
PX4添加新的应用
A scheme to improve the memory utilization of flutter
为 Docsify 自动生成 RSS 订阅
PMP考试通过心得分享
[computer network] learning notes, Part 3: data link layer (Xie Xiren version)
Xamarin deploys IOS from scratch Walterlv.CloudKeyboard application
Deeplight Technology Bluetooth protocol SRRC certification services
How does spotify drive data-driven decision making?
还不快看!对于阿里云云原生数据湖体系全解读!(附网盘链接)
Game optimization performance (11) - Zhihu
随机推荐
Istio流量管理--Ingress Gateway
Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
Flink的sink实战之一:初探
笔试面试题目:求丢失的猪
TiDB 性能竞赛 11.02-11.06
软件测试就是这么回事?!
Harbor项目高手问答及赠书活动
Japan PSE certification
Personal current technology stack
Analysis of istio access control
Web novice problem of attacking and defending the world
阿里出品!视觉计算开发者系列手册(附网盘链接)
Learning summary (about deep learning, vision and learning experience)
laravel8更新之速率限制改进
211考研失败后,熬夜了两个月拿下字节offer!【面经分享】
狗狗也能操作无人机!你没看错,不过这其实是架自动驾驶无人机 - 知乎
不多不少,大学里必做的五件事(从我的大一说起)
Deeplight Technology Bluetooth protocol SRRC certification services
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Flink's sink: a preliminary study