Today I will share with you short function, which send mails. It could be usable for example to sending reports or monitoring notifications. In this script I am using office 365 mailbox cofiguration.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function Send-ToEmail([string]$email){ $message = new-object Net.Mail.MailMessage; $message.From = "sender@exampledomain.pl"; $message.To.Add($email); $message.IsBodyHTML = "Service is stopped ! . "; #$message.Body = $bodymail; $message.Body = "please start the service."; $smtp = new-object Net.Mail.SmtpClient("smtp.office365.com", "587"); $smtp.EnableSSL = $true; $smtp.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password); $smtp.send($message); } |
In this case $bodymail is a table with disk space reports converted to html by using below command.
1 2 3 4 5 |
$alldisk = @() . . . $bodymail = $alldisks | ConvertTo-Html |
To send email we need to execute function with email address as a parameter.
1 |
Send-ToEmail -email "recipient@exampledomain.pl" |