Powershell发送带图像的电子邮件

时间:2016-10-31 05:11:59

标签: email powershell powershell-v3.0

我正在尝试将图片嵌入到通过Powershell发送的电子邮件中。

以下是我的代码:

$Attachment = New-Object Net.Mail.Attachment($LocalLocation)
$Attachment.ContentDisposition.Inline = $True
$Attachment.ContentDisposition.DispositionType = "Inline"
$Attachment.ContentType.MediaType = "image/png"


$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To.Add($emailTo)
$MailMessage.From = $MyEmail
$MailMessage.Subject = "Test Email"
$MailMessage.IsBodyHtml = $True
$MailMessage.Attachments.Add($Attachment)

$MailMessage.Body = "
  <html>
    <head></head>
    <body>
      <img src='CID:$($Attachment.ContentId)' />
    </body>
  </html>"

$SmtpClient = New-Object Net.Mail.SmtpClient("123.0.0.1",25 )
$SmtpClient.Send($MailMessage)

我收到了一封电子邮件,但邮件中只有一个空白框。 $ LocalLocation是我图像的链接。

我正在使用Powershell 3

2 个答案:

答案 0 :(得分:3)

$SendTo = "Sender Mail ID"
$SMTPServer = "SMTP Server" 
$EmailFrom = “Reciever Mail ID”
$EmailSubject = “Email including images in HTML”
$Image = "Image File"
$Message = new-object Net.Mail.MailMessage
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
$att = new-object Net.Mail.Attachment($Image)
$att.ContentId = "att"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$body = '<img src="cid:att" />'
$Message.From = $EmailFrom
$Message.To.Add($SendTo)
$Message.Subject = $EmailSubject
$Message.Body = $body
$Message.IsBodyHTML = $true
$Message.Attachments.Add($att)
$smtp.Send($Message)
$att.Dispose()

希望这个HEpls。

答案 1 :(得分:-1)

以下是使用上面的代码Send" with "1" argument(s): "Failure sending mail."

时的错误