任何想法为什么这个脚本在图像的电子邮件中给出红色X.

时间:2017-03-16 13:18:53

标签: powershell

Send-MailMessage -From "Snoopy <Snoopy@Brand.com>" -To"Jermaine <jmaine@Brand.com>" -Subject "Weekly Headcount Report" -Attachments "\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image001.png", "\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image002.png","\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image003.png","\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image004.png","\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image005.png","\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image006.png","\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image007.png","\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image008.png" -SmtpServer "smtp.Brand.com" -Body '<html> <body> <img src="cid:2017 snerk Headcount_25311_image001.png"><br> <img src="cid:2017 snerk Headcount_25311_image002"><br> <img src="cid:2017 snerk Headcount_25311_image003.png"><br> <img src="cid:2017 snerk Headcount_25311_image004.png"><br> <img src="cid:2017 snerk Headcount_25311_image005.png"><br> <img src="cid:2017 snerk Headcount_25311_image006.png"><br> <img src="cid:2017 snerk Headcount_25311_image007.png"><br> <img src="cid:2017 snerk Headcount_25311_image008.png"><br> </body> </html>' -BodyAsHtml

我尝试过各种不同的东西让它发挥作用,但到目前为止,无论如何我都会得到红色的X.我尝试了在SO和其他地方找到的这个脚本的不同修改。其中一个在那里有InlineAttachments并且powershell在该参数上引发了错误。附件在那里并没有损坏,但它们没有出现在体内。

我有powershell 5.0.10586.117

因为每个人都想指出我的帖子并说它是另一个人的精确副本,我将解决这个问题:

问题是我已经在https://gallery.technet.microsoft.com/scriptcenter/Send-MailMessage-3a920a6d复制了EXACT代码,但它无效。我没有收到任何错误,但我也没有收到任何电子邮件。

$images = @{ 
    image1 = '\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image001.png' 
    image2 = '\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image002.png'
    image3 = '\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image003.png'
    image4 = '\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image004.png'
    image5 = '\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image005.png'
    image6 = '\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image006.png'
    image7 = '\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image007.png'
    image8 = '\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\2017 snerk Headcount_25311_image008.png'
}  

$body = @' 
<html>  
  <body>  
    <img src="cid:image1"><br> 
    <img src="cid:image2"><br>
    <img src="cid:image3"><br> 
    <img src="cid:image4"><br> 
    <img src="cid:image5"><br> 
    <img src="cid:image6"><br>  
    <img src="cid:image7"><br> 
    <img src="cid:image8"><br> 
  </body>  
</html>  
'@  

$params = @{ 
    InlineAttachments = $images 
    Body = $body 
    BodyAsHtml = $true 
    Subject = "Test email" 
    From = 'Snoopy@Brand.com' 
    To = 'jmaine@Brand.com' 
    SmtpServer = 'smtp.Brand.com' 
} 

.\Send-MailMessage.ps1 @params

我也尝试过:

$files = get-childitem "\\portal2010.Brand.com\sites\snerk HC\snerk HC Dashboards\2017 JE Headcount_files\" | where name -like "*.png"
$body = @()
$attachments = @()
foreach($file in $files){
    $filename = [system.io.path]::GetFileName($file.FullName)
    $attachments += $file.fullname
    $body += "<br /><img src='" + $filename + "'/><br />"
}
$body = $body | Out-String
Send-MailMessage -to "Jermaine <jmaine@Brand.com>" -From "Snoopy <Snoopy@Brand.com>" -SmtpServer "smtp.Brand.com" -Subject "Test" -BodyAsHtml $body -Attachments $attachments

身体中的红色X.

所以是的,这可能就像那个问题,除了答案没有解决我的问题。

1 个答案:

答案 0 :(得分:0)

这是我最终得到并让它发挥作用的地方。

$SendTo = "email1@brand.com"
$CCTo1 = "email2@brand.com"
$CCTo2 = "email3@brand.com"
$CCTo3 = "email5@brand.com"
$CCTo4 = "email6@brand.com"
$SMTPServer = "smtp.brand.com" 
$EmailFrom = "email7@brand.com"
$EmailSubject = "Weekly HC Email"

$Image2 = '\\portal2010.brand.com\sites\HC\HC Dashboards\2017 JE HC_files\2017 HC_10419_image002.png'
$Image4 = '\\portal2010.brand.com\sites\HC\HC Dashboards\2017 JE HC_files\2017 HC_10419_image004.png'
$Image6 = '\\portal2010.brand.com\sites\HC\HC Dashboards\2017 JE HC_files\2017 HC_10419_image006.png'
$Image8 = '\\portal2010.brand.com\sites\HC\HC Dashboards\2017 JE HC_files\2017 HC_10419_image008.png'

$Message = new-object Net.Mail.MailMessage
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue

$att2 = new-object Net.Mail.Attachment($Image2)
$att2.ContentId = "att2"
$att4 = new-object Net.Mail.Attachment($Image4)
$att4.ContentId = "att4"
$att6 = new-object Net.Mail.Attachment($Image6)
$att6.ContentId = "att6"
$att8 = new-object Net.Mail.Attachment($Image8)
$att8.ContentId = "att8"

$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$body = '
        <img src="cid:att2" /><br/><br/>
        <img src="cid:att4" /><br/><br/>
        <img src="cid:att6" /><br/><br/>
        <img src="cid:att8" /><br/><br/>
'

$Message.From = $EmailFrom
$Message.To.Add($SendTo)
$Message.Cc.Add($CCTo1)
$Message.Cc.Add($CCTo2)
$Message.Cc.Add($CCTo3)
$Message.Cc.Add($CCTo4)
$Message.Subject = $EmailSubject
$Message.Body = $body
$Message.IsBodyHTML = $true
$Message.Attachments.Add($att2)
$Message.Attachments.Add($att4)
$Message.Attachments.Add($att6)
$Message.Attachments.Add($att8)
$smtp.Send($Message)