powershell - File not attaching to email in powershell script
问题描述
I am creating a CSV file and trying to attach it to an email. Also doing this in the cloud so I want to bypass local drives...just create the file and attach it. But I'm getting this error...
You cannot call a method on a null-valued expression. At line:67 char:1 $smtp.Attachments.Add($att.Name)
Am I not attaching it the right way? The email is sending but with no attachment. I know it's being created because when I put it in an Azure container I can see it. But I want to do this without storing it first. This seems right. Thank you!
$SqlAdapter.Fill($DataSet)
$att = New-Item filename.csv -ItemType file
$DataSet.Tables[0] | Export-CSV -NoTypeInformation $att
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$smtp.Attachments.Add($att)
$smtp.Send($From, $To, $subject, $body)
解决方案
SmtpClient 类将需要一个Net.Mail.Attachment
对象:
$att = New-Object Net.Mail.Attachment($file)
您构建对象而不是使用Send-MailMessage
cmdlet 的任何原因?
Send-MailMessage -To $To -From $From -Subject $Subject -Body $Body -SmtpServer $SmtpServer -Attachments $PathToAttachment
推荐阅读
- c++ - 为什么我在以下 C++ 代码中出现错误:错误:表达式列表在初始化程序中被视为复合表达式 [-fpermissive]
- java - 当 BorderPane.setLeft 出现 IndexOutOfBoundsException
- php - 如何在php中显示下午4点之前的下一个可用交货日期
- flutter - 如何通过 woocommerce rest api 在 json 文件中发布 line_items?
- angular - ng update 确实让我的角度应用程序升级到 11.0.0-next.6 但我想要稳定版本 10.x
- c# - 如何将 ASP.NET Core 和本地现有数据库发布到 Azure?
- html - 如何在没有空格的情况下在两侧之间留出空间
- python - 如何在 discord.py 中添加视频时间线?
- javascript - 查找字符串可以是数组
- google-chrome - Chrome 开发者工具如何配置 webworkers?