当前位置:网站首页>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
边栏推荐
- 临近双11,恶补了两个月成功拿下大厂offer,跳槽到阿里巴巴
- Can you do it with only six characters?
- Xamarin deploys IOS from scratch Walterlv.CloudKeyboard application
- Harbor项目高手问答及赠书活动
- 虚拟机中安装 macOS 11 big sur
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- PMP考试通过心得分享
- 解决Safari浏览器下载文件文件名称乱码的问题
- IQKeyboardManager 源代码看看
- 运维人员常用到的 11 款服务器监控工具
猜你喜欢

Game optimization performance (11) - Zhihu

游戏优化性能杂谈(十一) - 知乎

Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom

Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom

Recommend an economic science video, very valuable!

That's what software testing is all about?!

Entry level! Teach you how to develop small programs without asking for help (with internet disk link)

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...

OR Talk NO.19 | Facebook田渊栋博士:基于蒙特卡洛树搜索的隐动作集黑盒优化 - 知乎

Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
随机推荐
C语言I博客作业03
Harbor项目高手问答及赠书活动
不多不少,大学里必做的五件事(从我的大一说起)
The container with the most water
Web novice problem of attacking and defending the world
Written interview questions: find the smallest positive integer missing
虚拟机中安装 macOS 11 big sur
一个方案提升Flutter内存利用率
C语言I博客作业03
Recommend an economic science video, very valuable!
推荐一部经济科普视频,很有价值!
Japan PSE certification
python基本语法 变量
That's what software testing is all about?!
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
BCCOIN告诉您:年底最靠谱的投资项目是什么!
Architect (November 2020)
Test requirements for MIC certification of Bluetooth 2.4G products in Japan
入门级!教你小程序开发不求人(附网盘链接)
临近双11,恶补了两个月成功拿下大厂offer,跳槽到阿里巴巴