当前位置:网站首页>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
边栏推荐
- Xamarin deploys IOS from scratch Walterlv.CloudKeyboard application
- Written interview questions: find the smallest positive integer missing
- Analysis of istio access control
- 笔试面试题目:判断单链表是否有环
- 维图PDMS切图软件
- 413【毕设课设】基于51单片机无线zigbee无线智能家居光照温湿度传输监测系统
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- 蘑菇街电商交易平台服务架构及改造优化历程(含PPT)
- 值得一看!EMR弹性低成本离线大数据分析最佳实践(附网盘链接)
- 运维人员常用到的 11 款服务器监控工具
猜你喜欢
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Bccoin tells you: what is the most reliable investment project at the end of the year!
攻防世界之web新手题
PMP考试通过心得分享
YGC问题排查,又让我涨姿势了!
阿里出品!视觉计算开发者系列手册(附网盘链接)
Personal current technology stack
为什么 Schnorr 签名被誉为比特币 Segwit 后的最大技术更新
Japan PSE certification
Is software testing training class easy to find a job
随机推荐
2天,利用下班后的4小时开发一个测试工具
Japan PSE certification
Game optimization performance (11) - Zhihu
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Japan PSE certification
Bccoin tells you: what is the most reliable investment project at the end of the year!
2020-11-05
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
当Kubernetes遇到机密计算,看阿里巴巴如何保护容器内数据的安全!(附网盘链接)
来自朋友最近阿里、腾讯、美团等P7级Python开发岗位面试题
Ali! Visual computing developer's series of manuals (with internet disk link)
11 server monitoring tools commonly used by operation and maintenance personnel
413【毕设课设】基于51单片机无线zigbee无线智能家居光照温湿度传输监测系统
你搞不懂与别人的差距,永远成不了架构师!月薪15K和月薪65K,你差在那了?
分布式文档存储数据库之MongoDB基础入门
新的目标市场在哪里?锚定的产品是什么?| 十问2021中国企业服务
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Understanding design patterns
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...