【问题标题】:Powershell Send Email With ImagesPowershell 发送带有图像的电子邮件
【发布时间】:2019-08-16 21:34:04
【问题描述】:

我正在尝试将图像嵌入到要通过 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

【问题讨论】:

  • 在正文中您引用 $Attachment.ContentId 但我没有看到您正在为其分配值。

标签: email powershell powershell-3.0


【解决方案1】:
$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。

【讨论】:

  • IMO $mailmessage.Attachments.Add($att1) 不应放在 $mailmessage = New-Object system.net.mail.mailmessage 之前
  • 我想将图像嵌入到正文中。当我放入身体时,&lt;img src='CID:$($att1.ContentId)' /&gt; 仍然无法工作。谢谢
  • 编辑了脚本。现在您将能够在邮件正文中发送图像
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2013-06-01
    • 2015-07-29
    • 2013-11-11
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2015-12-24
    • 1970-01-01
    相关资源
    最近更新 更多