Powershell sending emails

This piece of code sends email form power shell.

function sendMail(){
#SMTP server name
$smtpServer = “smtpservername”;

#Creating a Mail object
$msg = new-object Net.Mail.MailMessage;

#Creating SMTP server object;
$smtp = new-object Net.Mail.SmtpClient($smtpServer);

#Email structure
$msg.From = "fromaddress@domain.com";
$msg.ReplyTo = "";
$msg.To.Add("email@domain.com");
$msg.subject = "My Subject";
$msg.body = "<HTML><B>Email Body</HTML>";
$msg.Priority = [System.Net.Mail.MailPriority]::High;#for sending email with high priority
$msg.isBodyHtml=$TRUE;# for sending html formatted emails
#Sending email
$msg.subject+="test";
$smtp.Send($msg);
write-host plain email sent;
}

$emailSent=sendmail(“”);

No comments:

Post a Comment