【问题标题】:How to attach files to a Powershell function and send it through MS Outlook?如何将文件附加到 Powershell 函数并通过 MS Outlook 发送?
【发布时间】:2023-03-24 08:57:01
【问题描述】:
我有这个代码:
Function Mailer ($MSubject, $MBody, $File){
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "abc@contoso.com"
$Mail.Subject = $MSubject
$Mail.Body = $MBody
$Mail.Attachments.Add($File)
$Mail.Send()
}
如果我通过将函数分配给 $File 变量来提供函数中的确切路径,我将工作。但是,我希望使这一点适用于不同的主题、身体和路径。我应该将文件路径设置为全局吗?你有什么想法?
在此先感谢 :)
彼得
【问题讨论】:
标签:
email
powershell
outlook
send
【解决方案1】:
这是我用来发送电子邮件的代码,
$from="ServerAdmin@Contoso.com"
$to="worker@contoso.com"
$cc=@("John.Doe@Contoso.com", "Jane.Doe@Contoso.com")
$subject="Weekly reports"
$attach="c:\SchTsk\Reports\Server_Inventory_$((Get-Date).ToString('MM-dd-yyyy')).csv","C:\SchTsk\Reports\VMPlatform_$((Get-Date).ToString('MM-dd-yyyy')).csv"
$body="This email contains weekly reports ran on the domain.<br>In an effort to reduce inbox spam, reports that generate separate files are now being attached to one weekly email."
Send-MailMessage -from $from -To $to -cc $cc -subject $subject -SmtpServer snmp.relay.contoso.com -Body $body -BodyAsHtml -Attachments $attach
【解决方案2】:
实际上该功能工作正常。问题出在我调用的文件路径的变量上。