gpt4 book ai didi

powershell - 将文件附加到电子邮件

转载 作者:行者123 更新时间:2023-12-02 23:54:28 24 4
gpt4 key购买 nike

我在Powershell中编写了以下内容,以尝试发送带有附件的电子邮件。

$FileDate = Get-Date
$SmtpServer = 'smtp.office365.com'
$SmtpUser = 'blah@blah.com'
$smtpPassword = 'blah'
$MailtTo = 'blah@blah.com'
$MailFrom = 'blah@blah.com'
$MailSubject = "The file for $FileDate is attached."
$MailBody = "Please take action"
$attachment= "C:\Users\my user\myfile.txt"
$attach = new-object Net.Mail.Attachment ($attachment)
$Credentials = New-Object System.Management.Automation.PSCredential -
ArgumentList $SmtpUser, $($smtpPassword | ConvertTo-SecureString -
AsPlainText -Force)

Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -
Body $MailBody -SmtpServer $SmtpServer -UseSsl -Credential $Credentials -
attachments "$attach"

当我执行它时,我在Powershell中收到以下错误
Send-MailMessage : Could not find file 
'C:\Users\myuser\System.Net.Mail.Attachment'.
At line:1 char:1
+ Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -
Body $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Send-MailMessage],
FileNotFoundException+ FullyQualifiedErrorId :
System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage

但是文件在那里是100%。谁能帮助我解决我的问题?

提前致谢

最佳答案

-AttachmentsSend-MailMessage参数需要一个字符串输入,因此我认为您根本不需要创建$attach变量,而只需使用$attachment即可,该文件已经包含要附加的文件的字符串路径:

.. -attachments $attachment

关于powershell - 将文件附加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48688585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com