当前位置:网站首页>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
边栏推荐
- Written interview topic: looking for the lost pig
- 年轻一代 winner 的程序人生,改变世界的起点藏在身边
- Solve Safari browser download file name garbled problem
- 我们采访了阿里云云数据库SQL Server的产品经理,他说了解这四个问题就可以了...
- PDMS cutting software
- Architect (November 2020)
- We interviewed the product manager of SQL server of Alibaba cloud database, and he said that it is enough to understand these four problems
- 如何将 PyTorch Lightning 模型部署到生产中
- [computer network] learning notes, Part 3: data link layer (Xie Xiren version)
- PCIe 枚举过程
猜你喜欢
随机推荐
Flink的sink实战之一:初探
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
阿里教你深入浅出玩转物联网平台!(附网盘链接)
为什么 Schnorr 签名被誉为比特币 Segwit 后的最大技术更新
Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
PMP考试通过心得分享
C语言I博客作业03
Analysis of ArrayList source code
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
2天,利用下班后的4小时开发一个测试工具
Rust : 性能测试criterion库
用科技赋能教育创新与重构 华为将教育信息化落到实处
PCIe 枚举过程
Deeplight Technology Bluetooth protocol SRRC certification services
Spotify是如何推动数据驱动决策的?
python小工具:编码转换
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
比Python快20%,就问你兴不兴奋?
That's what software testing is all about?!
Solve Safari browser download file name garbled problem

