Embed image in email using outlook object in powershell - html

I already posted a question related to this (HTML embedded image in the email not displayed ) and updated my script to include cid, but now my images are attached rather than coming inline. I am not sure how I can use inline images in outlook object.
$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
Then I am creating an outlook object and sending it with attachments in it.
$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()
It is attached without any issue, but not embedded inline. Can anyone help on this.
I dont have smtp server details and so I cannot use smtp client. So I tried to use this class System.Net.Mail.Attachment as below, but when I tried to add the attachment as below,
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)
I am getting this error -
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
Can someone assist me on how to use inline images in outlook object.?

You will need to set the PR_ATTACH_CONTENT_ID property to the matching value of the cid attribute (Volume.png in your case):
$attach = $mail.Attachments.Add($attachment)
$attach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "Volume.png")

Updated and Complete Answer incase if anyone needs :
$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()

Related

Post message to MS Teams via powershell

This is my first post here ,i hope i will get some help.
I have created a script in powershell for checking the expiry date of services principal in azure.The script itself works fine but now i want to post the output of script to MS Teams instead of generating a result file.
This is a piece of code.The result.list contains the output and i am loading it to $Body
$Body = get-content -Path .\result.list
$JSONBody = [PSCustomObject][Ordered]#{
"#type" = "text/plain"
"#context" = "http://schema.org/extensions"
"summary" = "Incoming Alert Test Message!"
"themeColor" = '0078D7'
"title" = "Incoming Alert Test Message!"
"text" = "$Body"
}
$TeamMessageBody = ConvertTo-Json $JSONBody -Depth 100
$parameters = #{
"URI" = '<uri>'
"Body" = $TeamMessageBody
"ContentType" = 'application/json'
}
Invoke-RestMethod #parameters
This i how the result.list looks like.E.g for a service-principal
DisplayName : sp-acr-ldl-pull
ObjectId : ***********
ApplicationId : ***********
KeyId : ***********
Type : Password
StartDate : 6/23/2020 2:29:20 PM
EndDate : 6/23/2021 2:29:16 PM
Status : Expired
But in Teams it is not human readable because the body is not passed correctly
DisplayName: sp-acr-ldl-pull ObjectId: ********** ApplicationId : ********** KeyId: *********** Type: Password StartDate: 6/23/2020 2:29:20PM EndDate
How can i format $JSONBody to get the same output in Teams same in result.list?
Basically, the problem is the line breaks, but you've got two related problems:
You're reading the contents of the file directly using Get-Content. By default in PowerShell this will return not a single string but rather an array of strings, one item for each line in the file. To get the contents as a single raw string, add the -Raw parameter at the end of the command.
By default, Teams is simply displaying your text without any formatting (including line breaks). If you want to send explicit line breaks, you need to tell Teams to do so, and you can do this using html "" tags (Teams message bodies support a limited subset of html). To do this, you can replace the line breaks in the $body variable with html <br> tags, like this: $Body = $Body.Replace("`r`n", "<br>").
Note that you're missing the "Method" parameter on your Invoke-RestMethod command.
Here is final working code, including the above:
$Body = get-content -Path "C:\temp\result.list" -Raw
$Body = $Body.Replace("`r`n", "<br>")
$JSONBody = [PSCustomObject][Ordered]#{
"#type" = "text/plain"
"#context" = "http://schema.org/extensions"
"summary" = "Incoming Alert Test Message!"
"themeColor" = '0078D7'
"title" = "Incoming Alert Test Message!"
"text" = "$Body"
}
$TeamMessageBody = ConvertTo-Json $JSONBody -Depth 100
$parameters = #{
"URI" = '<uri>'
"Body" = $TeamMessageBody
"ContentType" = 'application/json'
"Method" = "POST"
}
Invoke-RestMethod #parameters
So that will at least give you line breaks. If you want to preserve the "Table" style formatting, then I'd suggest using Adaptive Cards rather, perhaps something like the FactSet. You can learn how to send Adaptive Cards here, and here is a sample card using a FactSet, but you'd need to loop through the lines and build up the FactSet and the Adaptive Card, so hopefully the earlier solution is sufficient.

embedded html powershell email compressing length of pic [duplicate]

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.
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
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.
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. :)
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.

How to embed Images in a powershell email using MailMessage

I have an email that works from PS. What I have been trying to do is include images embedded in the email (not attachments). Below is what I have so far:
function Email
{
$smtpServer = {smtp server}
$smtpFrom = {email from}
$smtpTo = {email to}
$messageSubject = "test"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$credentials=new-object system.net.networkcredential({smtpUsername},{smtpPassword})
# $message.Body = Get-Content "D:\Program Files\CymbaTech_FBNC_AM\CTDataLoader\data\TestBody.html"
# The line below will add any attachments you have such as log files.
$message.Attachments.Add("{path}\Image1.png")
$message.Body = '<img src="cid:xyz">'
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.credentials=$credentials.getcredential($smtpserver,"25","basic")
$smtp.Send($message)
}
In the above, I have added image tags to the Body.html file. If I open the html directly, it looks as expected with images showing correctly.
When I send the mail however, the images are just displayed as white boxes with a border. Seems that the script is not loading the images in the file.
Has anyone done similar before and have any suggestions?
You must add images as regular attachments. The HTML body must then reference these attachments though the cid attribute: <img src="cid:xyz"> where "xyz" is the value of the Content-ID MIME attribute on the attachment MIME part.

Resolve DNS, export to Excel and HTML, then send mail

I was almost done with my script and did some late night editing and written over my old version so I cannot go back.
The script was running fine, still needed some tweaks but now it ha come to a complete halt.
The idea is to GC a list of IP's. Resolve the IP's and place them in an excel sheet. Then save the sheet to htm and xlsx. And finally mailing those to me.
Now it gets stuck on sorting the sheet, saving AND mailing...
Can someone give me some insight on what I did wrong here?
it gets stuck on sorting the sheet, saving AND mailing.
It no longer sorts B3:B$Count:
Exception calling "Sort" with "1" argument(s): "The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank."
At C:\Folder\Scripts\Get-IP.ps1:137 char:5
+ [void] $objRange.Sort($objRange2)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
It no longer saves the xlsx file, but does save the HTM file. It is clearly not overwriting something. I even restarted to make sure.
Exception calling "SaveAs" with "1" argument(s): "Microsoft Excel cannot access the file 'C://Folder/BlockedIP/HTML/2014-07-08/0BCEF810'. workbook."
At C:\Folder\Scripts\Get-IP.ps1:160 char:5
+ $b.SaveAs("$FileXML")
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
And finally, it will no longer send me e-mails:
New-Object : Exception calling ".ctor" with "2" argument(s): "The specified string is not in the form required for an e-mail address."
At C:\Folder\Scripts\Get-IP.ps1:217 char:13
+ $SMTP = New-Object System.Net.Mail.MailMessage($SMTP, 587)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
The script:
#Get current date
$Date = get-date -format yyyy-MM-dd
#Define all files/Paths.
$Path = "C:/Folder/BlockedIP"
md "$Path/HTML/$Date" -Force
$path2 = "$Path/HTML/$Date"
$PathWeb = "/HTML/$Date"
#Define File's used or created in this script.
$File = "$Path/IP-$Date.txt"
$FileHtml = "$Path2/IP-$Date.htm"
$FileXML = "$Path2/IP-$Date.xlsx"
$FileHTMLWeb = "$PathWeb/IP-$date.htm"
#Get content from given IP list.
$colComputers = #(get-content $File | Sort -unique)
$count = $colComputers.Count
write-output "$Count IP's detected."
#Define error actions.
#$erroractionpreference = "SilentlyContinue"
#Open Excel.
$a = New-Object -comobject Excel.Application
#Since we want this script to look like it's being used without excel I set it's visibility to false.
$a.visible = $True
#Disable excel confirmations.
$a.DisplayAlerts = $False
#Create sheets in Excel.
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
#Create a Title for the first worksheet and adjust the font
$row = 1
$Column = 1
target="_parent">Creator'
$c.Cells.Item($row,$column)= "Blocked IP's $Date"
$c.Cells.Item($row,$column).Font.Size = 18
$c.Cells.Item($row,$column).Font.Bold=$True
$c.Cells.Item($row,$column).Font.Name = "Cambria"
$c.Cells.Item($row,$column).Font.ThemeFont = 1
$c.Cells.Item($row,$column).Font.ThemeColor = 4
$c.Cells.Item($row,$column).Font.ColorIndex = 55
$c.Cells.Item($row,$column).Font.Color = 8210719
$range = $c.Range("a1","e1")
$range.Merge() | Out-Null
$range.VerticalAlignment = -4160
#Define subjects.
$c.Name = "Blocked IP's ($Date)"
$c.Cells.Item(2,1) = "Given IP"
$c.Cells.Item(2,2) = "Resolved DNS"
$c.Cells.Item(2,3) = "Returned IP"
$c.Cells.Item(2,5) = "Company name"
#Define cell formatting from subjects.
$c.Range("A2:E2").Interior.ColorIndex = 6
$c.Range("A2:E2").font.size = 13
$c.Range("A2:E2").Font.ColorIndex = 1
$c.Range("A2:E2").Font.Bold = $True
#Define the usedrange for autofitting.
$d = $c.UsedRange
#Make everything fit in it's cell
$D.EntireColumn.AutoFit() | Out-Null
#Define html code for Excel save to .htm.
$xlExcelHTML = 44
#Define rows to alter in excel.
$iRow = 3
$intRow = 3
#Time to run the script.
foreach ($strComputer in $colComputers)
{
#Place IP's from text in the excel sheet
$c.Cells.Item($intRow, 1) = $strComputer.ToUpper()
$d.EntireColumn.AutoFit() | Out-Null
#Create a status bar for the script
$i = 1
Write-Progress -Activity `
"Creating a usable 'Blocked IP' list ($i/$count)" `
-PercentComplete ($i/$colComputers.Count*100) `
-Status "Please stand by"
try {
$dnsresult = [System.Net.DNS]::GetHostEntry($strComputer)
}
catch {
$dnsresult = "$null"
}
#Clear screen on every checked IP to remove the 'True' statement.
#cls
#Do something with $dnsresults.
#Display information about host
#Give hostname Entry in Cell2
$c.Cells.Item($intRow,2) = $dnsresult.HostName
#IP listed in Cell 3
$c.Cells.Item($intRow,3) = $dnsresult.AddressList[0].IpAddressToString
#Make everything fit in it's cell.
$d.EntireColumn.AutoFit() | Out-Null
#Define row for the IP list.
$intRow = $intRow + 1
#Set background color for the IP list.
$d.Range("A$($iRow):E$($intRow)").interior.colorindex = 15
#Sort all IP's on resolved name.
$objWorksheet = $b.Worksheets.Item(1)
$objRange = $objWorksheet.UsedRange
$objRange2 = $objworksheet.Range("B3:B($Count)")
[void] $objRange.Sort($objRange2)
#Define borders here.
<# Insert script :D #>
#Define Filters here. (Picking out blank DNS and giving those a name)
<# Insert script :D #>
#Define Filters here. (Picking out specific DNS name and give them color code)
<# Insert script :D #>
#Make everything fit in it's cell.
$d.EntireColumn.AutoFit() | Out-Null
#Clear screen on every checked IP to remove the 'True' statement.
#cls
}
#Save the file as .xlsx on every placed IP to ensure the file is not lost due to any reason.
$b.SaveAs("$FileXML")
#Save final result as a .htm file
$b.SaveAs("$FileHTML",$xlExcelHTML)
#Close and quit Excel.
$b.Close()
get-process *Excel* | Stop-Process -force
#Move .txt file to the correct HTML folder.
move-item $file $path2 -Force
#Clear screen, again. (Let's keep things tidy.)
#cls
#Variables for public IP
# I am defining website url in a variable
$url = "http://checkip.dyndns.com"
# Creating a new .Net Object names a System.Net.Webclient
$webclient = New-Object System.Net.WebClient
# In this new webdownlader object we are telling $webclient to download the
# url $url
$IpPublic = $webclient.DownloadString($url)
# Just a simple text manuplation to get the ipadress form downloaded URL
# If you want to know what it contain try to see the variable $IpPublic
$IpPublic2 = $IpPublic.ToString()
$ipPublic3 = $IpPublic2.Split(" ")
$ipPublic4 = $ipPublic3[5]
$ipPublic5 = $ipPublic4.replace("</body>","")
$FinalIPAddress = $ipPublic5.replace("</html>","")
#Variables e-mail.
$From = "Blocked IP <###g##.com>"
$To = "IT Dept <#####.nl>"
$CC = "Someone <##r###.nl"
$SMTP = "smtp.gmail.com"
$Subject = "Blocked IPs for $date ($Count Total)"
#The href should point to the htm file in your iis/apache folder.
$WebLink = $FinalIPAddress+$FileHtmlWeb
$here = "<a href='http://$Weblink'><b>Here</b></a>"
#Define the body of your e-mail, in this case it displays a message and shows the server it is send from with it's local IP.
#A link to the .htm file, how many IP's were blocked and the date of the message.
$Body = "This is an automated message generated by server: $env:COMPUTERNAME, $IP</br></br>
Please see the attachment or click $here to get the $Count blocked IP's of $date. </br> </br></br>"
#Variables e-mail user.
$username = "#####.com"
$password = "##"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
$ip = (Get-WmiObject -class win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"').ipaddress[0]
#Clear screen, again. (Let's keep things tidy.)
#cls
#Send output as e-mail.
$SMTP = New-Object System.Net.Mail.MailMessage($SMTP, 587)
$SMTP.EnableSsl = $true
$SMTP.Credentials = New-Object System.Net.NetworkCredential("$username", "$password");
$SMTP.isbodyhtml= $true
$SMTP.Send($From, $To, $Subject, $FileXML, $Body)
send-mailmessage -BodyAsHtml -from $From -to $To -cc $CC -subject $Subject -Attachments $FileXML -body $Body -priority High -smtpServer $SMTP -credential ($cred) -usessl
#Create a function to relase Com object at end of script.
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(
[System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
#Release COM Object
[System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$a) | Out-Null
#Clear screen for the final time. (Let's keep things tidy.)
#cls
#Exit powershell
exit
Any help will be greatly appreciated!
Exception calling "Sort" with "1" argument(s): "The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank."
Double-check that $objRange and $objRange2 reference the correct ranges:
$objRange.Address()
$objRange2.Address()
Not much else I can tell you here without seeing your actual data.
Exception calling "SaveAs" with "1" argument(s): "Microsoft Excel cannot access the file 'C://Folder/BlockedIP/HTML/2014-07-08/0BCEF810'. workbook."
If the path really were C://Folder/... you'd be getting a different exception. Please do not fabricate error messages.
New-Object : Exception calling ".ctor" with "2" argument(s): "The specified string is not in the form required for an e-mail address."
You're confusing MailMessage and SmtpClient class. Not to mention that you don't even need either of them, since you're using Send-MailMessage anyway. Just remove the following 5 lines:
$SMTP = New-Object System.Net.Mail.MailMessage($SMTP, 587)
$SMTP.EnableSsl = $true
$SMTP.Credentials = New-Object System.Net.NetworkCredential("$username", "$password");
$SMTP.isbodyhtml= $true
$SMTP.Send($From, $To, $Subject, $FileXML, $Body)
Solved the sorting problem by altering the code to:
$objRange = $c.Range("A$($iRow):E$($intRow)")
$objRange2 = $c.Range("B$($iRow):B$($intRow)")
[void] $objRange.Sort($objRange2)
Turns out it got stuck on the header in the xml file
Replaced mailing with:
$From = "Blocked IP <#####.##>"
$To = "IT Dept <#####.##>"
$CC = "Someone <#####.##"
$Subject = "Blocked IPs for $date ($Count Total)"
#The href should point to the htm file in your iis/apache folder.
$WebLink = $FinalIPAddress+$FileHtmlWeb
$here = "<a href='http://$Weblink'><b>Here</b></a>"
#Define the body of your e-mail, in this case it displays a message and shows the server it is send from with it's local IP.
#A link to the .htm file, how many IP's were blocked and the date of the message.
$body = "Dear <font color=black>$to</font>,<br><br>"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "###gmail.com"
$Password = "##"
$message = New-Object System.Net.Mail.MailMessage
$message.IsBodyHTML = $true
$message.ReplyTo = $From
$message.Sender = $From
$message.subject = $subject
$message.body = $body
$message.to.add($to)
$message.from = $username
$message.attachments.add($MailXML)
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
The saving of the file turned out to be a misguided path.
Since the excel sheet was created from folder1 it wouldnt save instandly to folder 2 since its temp save file would remain in folder1.

Add HTML body to an email with Powershell

I'm using powershell to notify a high number of users about an expiration date of a document.
I have my script prepared
$smtpServer = "xxxxx.contoso.com"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = New-Object Net.Mail.mailaddress "xxxx#contoso.es", "xxx yyy zzz"
$msg.ReplyTo = "no-reply#contoso.es"
$msg.To.Add("xxx#contoso.es")
$msg.bcc.add($bcc)
$msg.subject = "Expiration notification"
$msg.body = I need to include HTML content, or MHT or bin as outlook message format.
$smtp.Send($msg)
But the email body was preformatted using outlook, so it contains images, preformated text etc. Which would be the best way to include that kind of content in the message body??
Thank you!
It should work if you add the Body as an html string and also add the line:
$msg.IsBodyHTML = true