Link to home
Start Free TrialLog in
Avatar of carbonbase
carbonbaseFlag for United Kingdom of Great Britain and Northern Ireland

asked on

sending email as html - PowerShell function

Hello, I have the following PowerShell function which sends an email, it sends email as plain text, I would like to update the function so that it sends email as html:

function SendMail ($SMTPserver,$sendermailaddress,$usermailaddress,$mailBody)            
{            
    $smtpServer = $SMTPserver            
    $msg = new-object Net.Mail.MailMessage            
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)            
    $msg.From = $sendermailaddress            
    $msg.To.Add($usermailaddress)            
    $msg.Subject = $mailSubject           
    $msg.Body = $mailBody     
    $smtp.Send($msg)              
}

# call SendMail 

 SendMail $SMTPserver $sendermailaddress $usermailaddress $mailBody          

Open in new window

Thanks in advance.
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

try set the IsBodyHtml property

MailMessage.IsBodyHtml Property

https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.isbodyhtml?view=net-5.0

$msg.IsBodyHTML = $true

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah 1st thought on this is , why are you using .NET when there's a built-in PS command that does that.

Use the Send-MailMessage as oBdA suggested.



Avatar of carbonbase

ASKER

Thanks to everyone for your comments!