Sending Email with Powershell

Sending email from powershell, With nothing installed on a windows 7-10 system you can send yourself email, and attachments using nothing but powershell.  This is useful if you have a dummy email account you want to be able to send system information from.

$ReportEmail.Subject = This will go in the subject line, in this case we are telling the system to put its hostname in the subject line.

$ReportEmail.Body = this content will go into the body of the email, in this case we are piping the content of a text file we made into the email body.

$SMTPServer = ‘smtp.mailserver.com‘ ; $SMTPInfo = New-Object Net.Mail.SmtpClient($SmtpServer, 587) ; $SMTPInfo.EnableSsl = $true ; $SMTPInfo.Credentials = New-Object System.Net.NetworkCredential(‘randomaccountyouown@some.com‘, ‘passwordforthataccount’); ; $ReportEmail = New-Object System.Net.Mail.MailMessage ; $ReportEmail.From = ‘randomaccountyouown@some.com‘ ; $ReportEmail.To.Add(‘recivingemailaccount@some.com’) ; $ReportEmail.Subject = Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name ; $ReportEmail.Body = Get-Content -Path C:\fileyouwanttoattach.txt -Raw ; $ReportEmail.Attachments.Add(‘c:\fileyouwanttoattach.txt‘) ; $SMTPInfo.Send($ReportEmail)

So this is pretty great you fill in the blanks and if all goes well you should be able to send yourself some email, some things that I should note, if your using this is some automated fashion you should probably make the email thats sending disposable.   You can attach almost any file as long as its 20MBs or smaller (this is subject to change depending on mail provider) make sure to stay within your email providers terms of service.

One of the really cool things you can do with this script is to use it in conjunction with other scripts to do things like pipe ipconfig into a text file on C: then attach that to an email to yourself to get the IP of that system.

#note that this script is more of a template for your ideas and is in no way free of bugs/typos, but it does work!

script as a preformated string
$SMTPServer = 'smtp.mailserver.com' ; $SMTPInfo = New-Object Net.Mail.SmtpClient($SmtpServer, portnumber) ; $SMTPInfo.EnableSsl = $true ; $SMTPInfo.Credentials = New-Object System.Net.NetworkCredential('randomaccountyouown@some.com', 'passwordforthataccount'); ; $ReportEmail = New-Object System.Net.Mail.MailMessage ; $ReportEmail.From = 'randomaccountyouown@some.com' ; $ReportEmail.To.Add(‘recivingemailaccount@some.com’) ; $ReportEmail.Subject = Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name ; $ReportEmail.Body = Get-Content -Path C:\fileyouwanttoattach.txt -Raw ; $ReportEmail.Attachments.Add(‘c:\fileyouwanttoattach.txt') ; $SMTPInfo.Send($ReportEmail)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s