How to send email on Windows PowerShell

We can send email to another email account on PowerShell.Let’s see this example:

$smtpServer = "smtp.gmail.com"
$smtpUser = "emailaddress@gmail.com"
$smtpPass = "password"
$mail = New-Object System.Net.Mail.MailMessage
$mail.From = New-Object System.Net.Mail.MailAddress($smtpUser)
$mail.To.Add("sendtoemail@hotmail.com")
$mail.Subject = "Hi"
$mail.Body = "Hello World!"
$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPass
$smtp.Send($mail)
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>