如何使用 PowerShell 将文件附加到电子邮件
我已经编写了一个 PowerShell 脚本来创建电子邮件,但我似乎无法附加文件。该文件确实存在并且 PowerShell 可以打开它,谁能告诉我我做错了什么?
$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 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您使用的是 PowerShell 2.0,只需使用内置 cmdlet Send-MailMessage:
如果您复制/粘贴此内容,请注意反引号后添加的额外空格。 PowerShell 不喜欢它。
If you are on PowerShell 2.0, just use the built-in cmdlet Send-MailMessage:
If you copy/paste this watch out for the extra space added after the backtick. PowerShell doesn't like it.
使上述工作正常工作
我通过删除该行并更改
为来
,虽然@Keith Hill 提供的解决方案会更好,但即使经过大量的护目镜,我也无法使其工作。
I got the above to work by removing the line
and changing
to
While the solution provided by @Keith Hill would be better, even with a lot of goggling I couldn't get it to work.
这对我使用 powershell-
定义变量有用:
使用脚本中的变量:
This worked for me using powershell-
Define Variables:
Use the variables in the script:
我遇到过这样的问题,(Windows 10 / PS 5.1)
我的 SMTP 未经过验证或安全...
我必须通过“MyAttacheObject.Dispose()”这一行来完成
.../终于可以工作了:!
这是我的代码,有两个附件:
I have experienced such problem, (windows 10 / PS 5.1)
My SMTP is not authentified or secure ...
I have to finish by this line "MyAttacheObject.Dispose()"
... / and finally that's work :!
this is my code with two attachments :
我需要删除“-AsPlainText -Force”
I needed to drop the "-AsPlainText -Force"
Send-MailMessge 已过时。
2024.06.13 我本来打算使用 Send-MailMessge 但看到 Microsoft 声明已过时。
Send-MailMessage cmdlet 已过时。此 cmdlet 不保证与 SMTP 服务器的安全连接。虽然 PowerShell 中没有立即可用的替代方案,但我们建议您不要使用 Send-MailMessage。
来源 2024.06.13:https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-7.4
Send-MailMessge is Obsolete.
2024.06.13 I was going to use Send-MailMessge but see Microsoft states is Obsolete.
The Send-MailMessage cmdlet is obsolete. This cmdlet doesn't guarantee secure connections to SMTP servers. While there is no immediate replacement available in PowerShell, we recommend you do not use Send-MailMessage.
Source 2024.06.13: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-7.4
您可以使用 send-mailmessage 或 system.net.mail.MailMessage 来完成它。有趣的是,两种方法之间存在显着的执行时间差异。您可以使用measure-command来观察命令的执行时间。
You can use send-mailmessage or system.net.mail.MailMessage to accomplish it. Interestingly, there is a significant execution time difference between the two approaches. You can use measure-command to observe the execution time of the commands.