内存中的 Send-MailMessage 附件
Send-MailMessage Attachment from memory
我正在尝试使用我在 this thread, which points to this URL 中找到的答案在 Send-MailMessage 中发送附件而不先保存到磁盘。
它说要使用:
$attachment = [System.Net.Mail.Attachment]::CreateAttachmentFromString($attachmenttext,"test.txt")
但是当我尝试使用 Send-MailMessage 而不是它显示的复杂方式来执行此操作时,我收到此错误:
Send-MailMessage -From "email@email.com" -To "email@email.com" -Subject "Subject" -Body $body -SmtpServer "smtp.server.local" -Port 25 -BodyAsHtml -Attachments $attachment
Send-MailMessage : Could not find file 'C:\Windows\system32\System.Net.Mail.Attachment'.
At line:3 char:1
+ Send-MailMessage -From "email@email.com" -To "email@e ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Send-MailMessage], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage
是否可以继续通过此方法使用 Send-MailMessage?或者是否有不同的方法允许我在不先保存到磁盘的情况下附加文件,同时仍然使用 send-mailmessage?
让我先回顾一下哪里出了问题。 [System.Net.Mail.Attachment]::CreateAttachmentFromString
输出类型 System.Net.Mail.Attachment
而 send-mailmessage -attachments
正在寻找字符串数组 string[]
。这就是示例失败的原因。
我编写了一个简单的函数,因此您可以使用您在示例中发布的附件创建一个 MailMessage
并查找 [System.Net.Mail.Attachment]
.
类型的附件
Function Try-SendMail([string[]]$To, [string]$From, [string]$SmtpServer, [int]$Port = 25, [pscredential]$SmtpCredential, [string]$Subject, [string]$Body, [System.Net.Mail.Attachment[]]$attachment, [switch]$IsBodyHTML){
[System.Net.Mail.MailMessage]$Mail = new-object System.Net.Mail.MailMessage
$To | %{$Mail.To.Add($_)}
$Mail.From = $From
$Mail.IsBodyHtml = $IsBodyHTML
$Mail.Body = $Body
$Mail.Subject = $Subject
$Attachment | %{$mail.Attachments.Add($_)}
[System.Net.Mail.SmtpClient]$SMTP = new-object System.Net.Mail.SmtpClient
$SMTP.Host = $SmtpServer
$SMTP.Port = $Port
If($SmtpCredential){
$NetCredential = New-Object System.Net.NetworkCredential
$NetCredential.UserName = $SmtpCredential.GetNetworkCredential().UserName
$NetCredential.Password = $SmtpCredential.GetNetworkCredential().Password
$SMTP.Credentials = $NetCredential
}
try{
$SMTP.Send($Mail)
}catch{
$_ | select *
}
}
你可以像这样使用它
$To = @("Person1@Test.com","Person2@Test.com")
$From = "MainGuy@PErson.com"
$Server = "SMTPSERVER.NET"
$Port = 587
$Attachments = @(
$([System.Net.Mail.Attachment]::CreateAttachmentFromString("HELLO", "Test.txt")),
$([System.Net.Mail.Attachment]::CreateAttachmentFromString("HELLO2", "Test2.txt"))
)
$SmtpCredential = Get-Credential
Try-SendMail -to $To -From $From -Subject "Hello" -Body "World" -Attachment $Attachments -SmtpServer $Server -port $Port -SmtpCredential $SmtpCredential
*编辑忘记在函数参数
中使[System.Net.Mail.Attachment]
到[System.Net.Mail.Attachment[]]
我正在尝试使用我在 this thread, which points to this URL 中找到的答案在 Send-MailMessage 中发送附件而不先保存到磁盘。
它说要使用:
$attachment = [System.Net.Mail.Attachment]::CreateAttachmentFromString($attachmenttext,"test.txt")
但是当我尝试使用 Send-MailMessage 而不是它显示的复杂方式来执行此操作时,我收到此错误:
Send-MailMessage -From "email@email.com" -To "email@email.com" -Subject "Subject" -Body $body -SmtpServer "smtp.server.local" -Port 25 -BodyAsHtml -Attachments $attachment
Send-MailMessage : Could not find file 'C:\Windows\system32\System.Net.Mail.Attachment'.
At line:3 char:1
+ Send-MailMessage -From "email@email.com" -To "email@e ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Send-MailMessage], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage
是否可以继续通过此方法使用 Send-MailMessage?或者是否有不同的方法允许我在不先保存到磁盘的情况下附加文件,同时仍然使用 send-mailmessage?
让我先回顾一下哪里出了问题。 [System.Net.Mail.Attachment]::CreateAttachmentFromString
输出类型 System.Net.Mail.Attachment
而 send-mailmessage -attachments
正在寻找字符串数组 string[]
。这就是示例失败的原因。
我编写了一个简单的函数,因此您可以使用您在示例中发布的附件创建一个 MailMessage
并查找 [System.Net.Mail.Attachment]
.
Function Try-SendMail([string[]]$To, [string]$From, [string]$SmtpServer, [int]$Port = 25, [pscredential]$SmtpCredential, [string]$Subject, [string]$Body, [System.Net.Mail.Attachment[]]$attachment, [switch]$IsBodyHTML){
[System.Net.Mail.MailMessage]$Mail = new-object System.Net.Mail.MailMessage
$To | %{$Mail.To.Add($_)}
$Mail.From = $From
$Mail.IsBodyHtml = $IsBodyHTML
$Mail.Body = $Body
$Mail.Subject = $Subject
$Attachment | %{$mail.Attachments.Add($_)}
[System.Net.Mail.SmtpClient]$SMTP = new-object System.Net.Mail.SmtpClient
$SMTP.Host = $SmtpServer
$SMTP.Port = $Port
If($SmtpCredential){
$NetCredential = New-Object System.Net.NetworkCredential
$NetCredential.UserName = $SmtpCredential.GetNetworkCredential().UserName
$NetCredential.Password = $SmtpCredential.GetNetworkCredential().Password
$SMTP.Credentials = $NetCredential
}
try{
$SMTP.Send($Mail)
}catch{
$_ | select *
}
}
你可以像这样使用它
$To = @("Person1@Test.com","Person2@Test.com")
$From = "MainGuy@PErson.com"
$Server = "SMTPSERVER.NET"
$Port = 587
$Attachments = @(
$([System.Net.Mail.Attachment]::CreateAttachmentFromString("HELLO", "Test.txt")),
$([System.Net.Mail.Attachment]::CreateAttachmentFromString("HELLO2", "Test2.txt"))
)
$SmtpCredential = Get-Credential
Try-SendMail -to $To -From $From -Subject "Hello" -Body "World" -Attachment $Attachments -SmtpServer $Server -port $Port -SmtpCredential $SmtpCredential
*编辑忘记在函数参数
中使[System.Net.Mail.Attachment]
到[System.Net.Mail.Attachment[]]