如何使用PowerShell将文件附加到电子邮件 [英] How to attach a file to an email with PowerShell

查看:105
本文介绍了如何使用PowerShell将文件附加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了将创建电子邮件的PowerShell脚本,但是似乎无法附加文件.该文件确实存在,PowerShell可以打开它,有人可以告诉我我在做什么错吗?

I have written a PowerShell script that will create an email, however I can't seem to attach a file. The file does exist and PowerShell can open it, Could anyone tell me what I'm doing wrong?

$ol = New-Object -comObject Outlook.Application 
$message = $ol.CreateItem(0)
$message.Recipients.Add("Deployment")  
$message.Subject = "Website deployment"  
$message.Body = "See attached file for the updates made to the website`r`n`r`nWarm Regards`r`nLuke"

# Attach a file this doesn't work
$file = "K:\Deploy-log.csv"
$attachment = new-object System.Net.Mail.Attachment $file
$message.Attachments.Add($attachment)

推荐答案

我通过删除该行使上面的内容起作用

I got the above to work by removing the line

$attachment = new-object System.Net.Mail.Attachment $file

并更改

$message.Attachments.Add($attachment)

$message.Attachments.Add($file)

尽管@Keith Hill提供的解决方案会更好,但是即使有很多眼神,我也无法解决它.

While the solution provided by @Keith Hill would be better, even with a lot of goggling I couldn't get it to work.

这篇关于如何使用PowerShell将文件附加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆