4

I have a powershell script that embedds (not attaches) a picture and sends an email. The picture has increased now to 1500x5000 pixels and now I'm seeing that the pictures lenth gets compressed and it distorts the picture. How ever, when I manually insert the picture via outlook and send an email, it looks fine.

If i save the picture and then open it via paint or something, the picture opens fine. It just looks compressed in the email. Anyone know what may be going on there?

{
    $Application = "C:\Autobatch\Spotfire.Dxp.Automation.ClientJobSender.exe"
    $Arguments  = "http://s.net:8070/spotfireautomation/JobExecutor.asmx C:\Autobatch\HourlyStats.xml"
    $CommandLine = "{0} {1}" -f $Application,$Arguments
    invoke-expression $CommandLine
    $file = "C:\Autobatch\HourlyStats.png"
    $smtpServer = "smtp.staom.sec.s.net"
    $att = new-object Net.Mail.Attachment($file)
    $att.ContentType.MediaType = “image/png”
    $att.ContentId = “pict”
    $att.TransferEncoding = [System.Net.Mime.TransferEncoding]::Base64
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg.Attachments.Add($att)
    $msg.From = "d.k@s.com"
    $msg.To.Add("r.p@p.com")
    $msg.Subject = "Voice and Data Hourly Stats"
    $msg.Body = "<p style=’font-family: Calibri, sans-serif’>
              Voice and data hourly stats details<br />
             </p>
             <img src='cid:pict'/>"
    $msg.IsBodyHTML = $true
    $smtp.Send($msg)
    $att.Dispose()
    invoke-expression "DEL $file"
}

here is what the picture looks like in the email.compressed pic

lightweight
  • 3,227
  • 14
  • 79
  • 142
  • is this an SMTP issue? If I right click on the pic in the email and save it and then open with preview, it looks fine. This is odd to me, why is it showing up compressed in the email then? – lightweight Jul 10 '13 at 15:08
  • Still not able to solve this issue...anyone have any ideas? – lightweight Jul 22 '13 at 13:44
  • It seems to me like a mail client issue. For me (Lotus Notes 8.5 client) the picture shows at full size sending message with your code. I tried to add `height='100%' width='100%'` to the `img` tag but it uses percent value from current window and scales the image to the window. The parameter `$att.ContentDisposition.Inline = $true` below removes the image from attachment and only shows as ebedded but not attached. Without this flag the picture is embedded AND attached at the same time. – n01d Jul 20 '16 at 14:21

4 Answers4

2

Try adding

$att.ContentDisposition.Inline = $true

I suspect some default behavior is happening under the covers and it's just not consistent between the script and Outlook.

More info here

philselmer
  • 751
  • 4
  • 22
  • sorry, no go on that, got the same result. Whats even weird is that if I manually copy the picture from viewer and paste it into outlook and send it, it works fine. – lightweight Sep 04 '13 at 11:11
2

It seems like your email client shrinks content to a certain maximum size. Try putting <img src='cid:pict'/> in a <div> environment:

<div style="overflow: scroll">
  <img src='cid:pict'/>
</div>

Also, if you have any way to retrieve the actual pixel width of the image, you can try to set the CSS of the <img> tag accordingly.

Bart
  • 1,600
  • 1
  • 13
  • 29
0

By asking this I may sound like a noob, but Just out of Curiosity, if you have a manual way to send an email via Outlook, why not to make a script to send an automated email with desired screenshot?

IDK, if this might help you or not, but I had made this script long back, for my daily reporting purposes. Well, it fits the bill. Sharing it here, for your views on it.

#In this segment, I navigate IE to my specific destination, screen which I want to capture.

$ie = New-Object -ComObject InternetExplorer.Application
$ie.Visible = $true;
$Website = $ie.navigate('https://put.your.URL.here')
while($Website.Busy){Start-Sleep -Seconds 5}

#In this class, script captures the screen, once, all the data loading is over.
$file = "C:\Users\Desktop\$(Get-Date -Format dd-MM-yyyy-hhmm).bmp"
#P.S. I made it to save that screenshot with current date and time format. Also, default screenshot will be captured in .BMP format. 

Add-Type -AssemblyName System.Windows.Forms
Add-type -AssemblyName System.Drawing

$Screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$width = $Screen.width
$Height = $Screen.Height
$Left = $Screen.Left
$Right = $Screen.Right
$Top = $Screen.Top
$Bottom = $Screen.Bottom

$bitmap = New-Object System.Drawing.Bitmap $width, $Height

$Graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$Graphics.CopyFromScreen($Left, $Top, 0, 0, $bitmap.Size)

$bitmap.Save($File) 

Write-Output "Screenshot saved to:"
Write-Output $File

sleep -Seconds 5

#Sending an Email
$Outlook = New-Object -ComObject Outlook.Application
$mail = $Outlook.CreateItem(0)
$mail.To = "your.designated@emailid.com"
$mail.Subject = "Outstanding data as on $(Get-Date -Format dd-MM-yyyy)"
$mail.Body = "PFA screenshot, of all outstanding formation as on $(Get-Date -Format dd-MM-yyyy-hhmm)"
$mail.Attachments.Add($file)
$mail.Send()

I am just answering this, since, I tried commenting above, but I guess, my reputation score is way too less to do that. Hope this might be helpful for you to find a workaround. Happy coding. :)

Ashish
  • 250
  • 3
  • 9
-2

HTML code: is <img src='cid:pict'/> supposed to be <img src='cid:pict'> -just remove the forward slash?

Added: This link might help talking about embedding pic in email. base64 encoded images in email signatures. You can try generation base64 code and put it in email body HTML.

Community
  • 1
  • 1
Peter
  • 196
  • 1
  • 10