当前位置:网站首页>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
边栏推荐
- Hematemesis! Alibaba Android Development Manual! (Internet disk link attached)
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- 糟糕,系统又被攻击了
- IQKeyboardManager 源代码看看
- 第二次作业
- 2 days, using 4 hours after work to develop a test tool
- Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
- C语言I博客作业03
- 2020-11-05
- Ali teaches you how to use the Internet of things platform! (Internet disk link attached)
猜你喜欢
Px4 adds new applications
It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)
If you don't understand the gap with others, you will never become an architect! What's the difference between a monthly salary of 15K and a monthly salary of 65K?
C语言I博客作业03
Flink's sink: a preliminary study
临近双11,恶补了两个月成功拿下大厂offer,跳槽到阿里巴巴
入门级!教你小程序开发不求人(附网盘链接)
Spotify是如何推动数据驱动决策的?
Rust : 性能测试criterion库
ArrayList源码分析
随机推荐
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
值得一看!EMR弹性低成本离线大数据分析最佳实践(附网盘链接)
Rust:命令行参数与环境变量操作
IQKeyboardManager 源代码看看
TCP协议如何确保可靠传输
Adobe Lightroom /Lr 2021软件安装包(附安装教程)
笔试面试题目:盛水最多的容器
Tidb performance competition 11.02-11.06
Second assignment
Installing MacOS 11 Big Sur in virtual machine
Mozi college SQL injection solution
仅用六种字符来完成Hello World,你能做到吗?
Deeplight Technology Bluetooth protocol SRRC certification services
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Hematemesis! Alibaba Android Development Manual! (Internet disk link attached)
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Deeplight Technology Bluetooth protocol SRRC certification services
比Python快20%,就问你兴不兴奋?
最全!阿里巴巴经济体云原生实践!(附网盘链接)