当前位置:网站首页>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
边栏推荐
- TiDB 性能竞赛 11.02-11.06
- 一个方案提升Flutter内存利用率
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- C language I blog assignment 03
- Bccoin tells you: what is the most reliable investment project at the end of the year!
- Deeplight Technology Bluetooth protocol SRRC certification services
- Analysis of istio access control
- 如何将 PyTorch Lightning 模型部署到生产中
- Function periodic table filter value selectedvalue
猜你喜欢

笔试面试题目:求缺失的最小正整数

临近双11,恶补了两个月成功拿下大厂offer,跳槽到阿里巴巴

Deeplight Technology Bluetooth protocol SRRC certification services
![[computer network] learning notes, Part 3: data link layer (Xie Xiren version)](/img/b0/b236a52e38f1cd3eff25a398dac7aa.jpg)
[computer network] learning notes, Part 3: data link layer (Xie Xiren version)

It's 20% faster than python. Are you excited?

第二次作业

It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)

Oops, the system is under attack again

VC + + specified directory file output by time

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
随机推荐
笔试面试题目:判断单链表是否有环
一个方案提升Flutter内存利用率
Flink's sink: a preliminary study
print( 'Hello,NumPy!' )
TCP协议如何确保可靠传输
Flink的sink实战之一:初探
Personal current technology stack
Istio流量管理--Ingress Gateway
Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
用科技赋能教育创新与重构 华为将教育信息化落到实处
What can your cloud server do? What is the purpose of cloud server?
Deeplight Technology Bluetooth protocol SRRC certification services
It's 20% faster than python. Are you excited?
值得一看!EMR弹性低成本离线大数据分析最佳实践(附网盘链接)
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Function periodic table filter value selectedvalue
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
VC++指定目录下文件按时间排序输出
2天,利用下班后的4小时开发一个测试工具
Flink's sink: a preliminary study