使用powershell

时间:2015-09-23 04:28:16

标签: html email powershell outlook

我已经发布了一个与此相关的问题(HTML embedded image in the email not displayed)并更新了我的脚本以包含cid,但现在我的图片是附加的而不是内联的。我不确定如何在outlook对象中使用内联图像。

$Text --> "<br /><font face='Arial'><b><i>For Full Interactive Viewing <a href=http://www.google.com>Click Here</a></i></b></font><br/>"

$MessageImages --> <br/><img src='cid:Volume.png'/><br/><br/><hr><br/><img src='C:\MyImages\Value.png'/><br/><br/><hr><br/><hr>

$FinalText = $Text+$MessageImages

然后我正在创建一个outlook对象并在其中发送附件。

$o = New-Object -com Outlook.Application

$mail = $o.CreateItem(0)

$mail.importance = 2

$mail.subject = “Reports - Automated Email from user list “

$mail.HTMLBody = $FinalText

$mail.SentOnBehalfOfName ="*********"

$mail.To = "*******"

$mail.Attachments.Add($file.ToString())

$mail.Send()

它没有任何问题附加,但没有嵌入内联。任何人都可以帮助这个。

我没有smtp服务器详细信息,所以我不能使用smtp客户端。所以我尝试使用此类System.Net.Mail.Attachment,如下所示,但当我尝试添加如下附件时,

PS H:\> $file


    Directory: C:\Users\Images


Mode                LastWriteTime     Length Name                                                                                                                                                                                                                  
----                -------------     ------ ----                                                                                                                                                                                                                  
-a---       23/09/2015  4:04 p.m.     132361 Volume.png   


 $attachment = New-Object System.Net.Mail.Attachment –ArgumentList ($file.FullName).ToString() 
 $attachment.ContentDisposition.Inline = $True
 $attachment.ContentDisposition.DispositionType = "Inline"
 $attachment.ContentType.MediaType = "image/png"
 $attachment.ContentId = $file.Name.ToString()
 $mail.Attachments.Add($attachment)

我收到此错误 -

Exception calling "Add" with "1" argument(s): "Value does not fall within the expected range."
At C:\work\Automated.ps1:130 char:23
+  $mail.Attachments.Add <<<< ($attachment)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

有人可以帮我解决如何在outlook对象中使用内嵌图像的问题吗?

2 个答案:

答案 0 :(得分:1)

您需要将PR_ATTACH_CONTENT_ID属性设置为cid属性的匹配值(在您的情况下为Volume.png):

private Cadastro cadastro = new Cadastro();

答案 1 :(得分:0)

如果有人需要,请更新并完成答案:

$Text = "<br /><font face='Arial'><b><i>For Full Interactive Viewing <a 
href=http://www.google.com>Click Here</a></i></b></font><br/>"

$MessageImages = "<br/><img src='C:\Users\E5559405\Desktop\IT\Image.jpg'/><br/>"

$FinalText = $Text+$MessageImages

$o = New-Object -ComObject Outlook.Application

$mail = $o.CreateItem(0)

$mail.importance = 2

$mail.subject = “Reports - Automated Email from user list “

$mail.HTMLBody = $FinalText

$mail.To = "xyz@gmail.com"

$mail.Send()