9/20/16

Sending e-mail with Powershell

To send an e-mail, basicly your computer connects to the remote mail server and talks to it using SMTP (Simple Mail Transfer Protocol).

When you use an e-mail client like MS Outlook, the mail client makes all of them for you, however when you make a script and want it send e-mail for alerting, it is not possible to do it with an e-mail client. So, a script like below will solve your issue; here, I do not write explanation of some of the lines since most of the variable names are explaining what it is used for.




########### SMTP SERVER AND MAIL SETTINGS #############

$smtpServer = "smtp.secureeoposts.blogspot.com"
$smtpPort = 587  #It depends according to your smtp server
$sslEnabled= $true  #If your smtp server does not support secure connections,
                                 #you have to delete this line
$username = sender@secureeo.blogspot.com
$password = "************"  #Type your password here
$from = sender@secureeo.blogspot.com
$to = receipent@secureeo.blogspot.com
$subject = "Test e-mail with Powershell"
$body = "This is a test e-mail sending with using Powershell"



###############  E-MAIL SETTINGS ###############

$smtp = new-object Net.Mail.SmtpClient($smtpServer, $smtpPort)
$smtp.sslEnabled = $sslEnabled #If your smtp server does not support secure
                                                     #connections, you have to delete this line
$smtp.Credentials = new-object Net.NetworkCredential($username, $password)
               $msg = new-object Net.Mail.MailMessage
               $msg.From = $from
               $msg.To.Add($to)
               $msg.Subject = $subject
               $msg.Body = $body
               $attachFile = "C:\Users\securityposts_PC\Desktop\email_test\email_test.PNG"
               $att = new-object Net.Mail.Attachment($attachFile)
               $msg.Attachments.Add($att)
               $smtp.Send($msg) 

No comments:

Post a Comment