December 13, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

Send email with attachment in powershell

$Body = “get the information here to show the data with attachement”  | Set-Content C:\somename.html

$file = “C:\somename.html”
$EmailFrom = “FromMail@Domain.com”
$EmailTo = “ToMail@Domain.com”
$SMTPServer = “Your Smtp Server Name”
$EmailSubject = “Enter Your Subject”
$att = new-object Net.Mail.Attachment($file)

#Send mail with attachment
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($EmailFrom)
$mailmessage.To.add($EmailTo)
$mailmessage.Subject = $EmailSubject
$mailmessage.Body = $Body
$mailmessage.IsBodyHTML = $true
$mailmessage.Attachments.Add($att)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer)
$SMTPClient.Send($mailmessage)
$att.Dispose()

$Body = “get the information here to show the data with attachement”  | Set-Content C:\somename.html

$file = “C:\somename.html”
$EmailFrom = “FromMail@Domain.com”
$EmailTo = “ToMail@Domain.com”
$SMTPServer = “Your Smtp Server Name”
$EmailSubject = “Enter Your Subject”
$att = new-object Net.Mail.Attachment($file)

#Send mail with attachment
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($EmailFrom)
$mailmessage.To.add($EmailTo)
$mailmessage.Subject = $EmailSubject
$mailmessage.Body = $Body
$mailmessage.IsBodyHTML = $true
$mailmessage.Attachments.Add($att)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer)
$SMTPClient.Send($mailmessage)
$att.Dispose()