当前位置:网站首页>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
边栏推荐
- TCP协议如何确保可靠传输
- [computer network] learning notes, Part 3: data link layer (Xie Xiren version)
- 在51CTO学院Get到PMP证书
- PMP心得分享
- 2020-11-05
- Flink的sink实战之一:初探
- OR Talk NO.19 | Facebook田渊栋博士:基于蒙特卡洛树搜索的隐动作集黑盒优化 - 知乎
- The most complete! Alibaba economy cloud original practice! (Internet disk link attached)
- 软件测试培训班出来好找工作么
- 软件测试就是这么回事?!
猜你喜欢

What is the difference between vivoy73s and vivoy70s

Is software testing training class easy to find a job

Can you do it with only six characters?

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

2 days, using 4 hours after work to develop a test tool

Solve Safari browser download file name garbled problem

Recommend an economic science video, very valuable!

That's what software testing is all about?!

Installing MacOS 11 Big Sur in virtual machine

Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
随机推荐
python基本语法 变量
吐血整理!阿里巴巴 Android 开发手册!(附网盘链接)
2020-11-05
Flink的sink实战之一:初探
VC + + specified directory file output by time
How does spotify drive data-driven decision making?
Second assignment
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
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?
Deeplight Technology Bluetooth protocol SRRC certification services
Rust:命令行参数与环境变量操作
python小工具:编码转换
Installing MacOS 11 Big Sur in virtual machine
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Flink的sink实战之一:初探
Architect (November 2020)
虚拟机中安装 macOS 11 big sur
Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
Automatically generate RSS feeds for docsify