如何使用PowerShell将文件附加到电子邮件

时间:2010-10-22 13:35:30

标签: email powershell attachment powershell-v2.0

我编写了一个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)

5 个答案:

答案 0 :(得分:13)

如果您使用的是PowerShell 2.0,只需使用内置的cmdlet Send-MailMessage:

C:\PS>Send-MailMessage -from "User01 <user01@example.com>" `
                       -to "User02 <user02@example.com>", `
                           "User03 <user03@example.com>" `
                       -subject "Sending the Attachment" `
                       -body "Forgot to send the attachment. Sending now." `
                       -Attachment "data.csv" -smtpServer smtp.fabrikam.com

如果您将此手表复制/粘贴,以便在反引号后添加额外空格。 PowerShell不喜欢它。

答案 1 :(得分:5)

我通过删除行

来完成上述工作
$attachment = new-object System.Net.Mail.Attachment $file

并改变

$message.Attachments.Add($attachment)

$message.Attachments.Add($file)

虽然@Keith Hill提供的解决方案会更好,但即使有很多镜头,也无法让它发挥作用。

答案 2 :(得分:2)

这对我使用powershell -

起作用了

定义变量:

$fromaddress = "donotreply@pd.com" 
$toaddress = "test@pd.com" 
$Subject = "Test message" 
$body = "Please find attached - test"
$attachment = "C:\temp\test.csv" 
$smtpserver = "mail.pd.com" 

使用脚本中的变量:

$message = new-object System.Net.Mail.MailMessage 
$message.From = $fromaddress 
$message.To.Add($toaddress)
$message.IsBodyHtml = $True 
$message.Subject = $Subject 
$attach = new-object Net.Mail.Attachment($attachment) 
$message.Attachments.Add($attach) 
$message.body = $body 
$smtp = new-object Net.Mail.SmtpClient($smtpserver) 
$smtp.Send($message)

答案 3 :(得分:0)

您可以使用send-mailmessage或system.net.mail.MailMessage来完成它。有趣的是,这两种方法之间存在显着的执行时间差异。您可以使用measure-command来观察命令的执行时间。

答案 4 :(得分:0)

我遇到了这样的问题,(Windows 10 / PS 5.1) 我的SMTP未通过身份验证或不安全... 我必须完成这一行“ MyAttacheObject.Dispose()”  ... /终于可以了:!

$smtp = new-object Net.Mail.SmtpClient($smtpserver) 
$attach.Dispose()

这是我的代码,带有两个附件:

# Email configuration NO AUTH NO SECURE
$emailHost = "smtp.bot.com"
$emailUser = ""
$emailPass = ""
$emailFrom = "myemail@bot.com"
$emailsTo=@("toyoumylove@bot.com","toyoumybad@bot.com")
$emailSubject = $title
$emailbody=$body
$attachment1 = @($PATh+$outFile) 
$attachment2 = @($PATh+$inFile) 
#End of parameters

$msg = New-Object System.Net.Mail.MailMessage
$msg.from = ($emailFrom)
    foreach ($d in $emailsTo) {    
    $msg.to.add($d)
    }
$msg.Subject = $emailSubject
$msg.Body = $emailbody
$msg.isBodyhtml = $true   

$att = new-object System.Net.Mail.Attachment($attachment1)
$msg.Attachments.add($att)
$att = new-object System.Net.Mail.Attachment($attachment2)
$msg.Attachments.add($att)
$smtp = New-Object System.Net.Mail.SmtpClient $emailHost
$smtp.Credentials = New-Object System.Net.NetworkCredential($emailUser, $emailPass);
  $smtp.send($msg)
  $att.Dispose()