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

发布于 2024-09-28 11:49:04 字数 532 浏览 3 评论 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)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

戏舞 2024-10-05 11:49:04

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

C:\PS>Send-MailMessage -from "User01 <[email protected]>" `
                       -to "User02 <[email protected]>", `
                           "User03 <[email protected]>" `
                       -subject "Sending the Attachment" `
                       -body "Forgot to send the attachment. Sending now." `
                       -Attachment "data.csv" -smtpServer smtp.fabrikam.com

如果您复制/粘贴此内容,请注意反引号后添加的额外空格。 PowerShell 不喜欢它。

If you are on PowerShell 2.0, just use the built-in cmdlet Send-MailMessage:

C:\PS>Send-MailMessage -from "User01 <[email protected]>" `
                       -to "User02 <[email protected]>", `
                           "User03 <[email protected]>" `
                       -subject "Sending the Attachment" `
                       -body "Forgot to send the attachment. Sending now." `
                       -Attachment "data.csv" -smtpServer smtp.fabrikam.com

If you copy/paste this watch out for the extra space added after the backtick. PowerShell doesn't like it.

可可 2024-10-05 11:49:04

使上述工作正常工作

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

我通过删除该行并更改

$message.Attachments.Add($attachment)

为来

$message.Attachments.Add($file)

,虽然@Keith Hill 提供的解决方案会更好,但即使经过大量的护目镜,我也无法使其工作。

I got the above to work by removing the line

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

and changing

$message.Attachments.Add($attachment)

to

$message.Attachments.Add($file)

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

情绪操控生活 2024-10-05 11:49:04

这对我使用 powershell-

定义变量有用:

$fromaddress = "[email protected]" 
$toaddress = "[email protected]" 
$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)

This worked for me using powershell-

Define Variables:

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

Use the variables in the script:

$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)
酸甜透明夹心 2024-10-05 11:49:04

我遇到过这样的问题,(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 = "[email protected]"
$emailsTo=@("[email protected]","[email protected]")
$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()

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 :!

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

this is my code with two attachments :

# Email configuration NO AUTH NO SECURE
$emailHost = "smtp.bot.com"
$emailUser = ""
$emailPass = ""
$emailFrom = "[email protected]"
$emailsTo=@("[email protected]","[email protected]")
$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()
萌梦深 2024-10-05 11:49:04
"yourpassword" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "D:\Password.txt"

# The above command will encrypt the password you need to run this command only one time

# 1.Sign in to your work or school account, go to the My Account page, and select Security info.
2.Select Add method, choose App password from the list, and then select Add.
3.Enter a name for the app password, and then select Next. it will give password 
# you should use this password in above mentioned command

$User = "[email protected]"
$File = "C:\Users\username\Desktop\Mail\Password.txt"
$cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString -AsPlainText -Force)
$EmailTo = "[email protected]"
$EmailFrom = "[email protected]"
$Subject = "SERVICE STOPPED"
$Body = "SERVICE STOPPED PFA Document to get more details."
$SMTPServer = "smtp.office365.com"
$filenameAndPath = "C:\Users\username\Desktop\new.txt"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
$SMTPClient.Send($SMTPMessage)
"yourpassword" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "D:\Password.txt"

# The above command will encrypt the password you need to run this command only one time

# 1.Sign in to your work or school account, go to the My Account page, and select Security info.
2.Select Add method, choose App password from the list, and then select Add.
3.Enter a name for the app password, and then select Next. it will give password 
# you should use this password in above mentioned command

$User = "[email protected]"
$File = "C:\Users\username\Desktop\Mail\Password.txt"
$cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString -AsPlainText -Force)
$EmailTo = "[email protected]"
$EmailFrom = "[email protected]"
$Subject = "SERVICE STOPPED"
$Body = "SERVICE STOPPED PFA Document to get more details."
$SMTPServer = "smtp.office365.com"
$filenameAndPath = "C:\Users\username\Desktop\new.txt"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
$SMTPClient.Send($SMTPMessage)
|煩躁 2024-10-05 11:49:04

我需要删除“-AsPlainText -Force”

$emailCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $emailUser, (Get-Content $emailPasswordFile | ConvertTo-SecureString)

I needed to drop the "-AsPlainText -Force"

$emailCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $emailUser, (Get-Content $emailPasswordFile | ConvertTo-SecureString)
过气美图社 2024-10-05 11:49:04

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

云醉月微眠 2024-10-05 11:49:04

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文