Tuesday, October 20, 2015

Powershell - Send Email through GMail SMTP Server

I came across an instance where I had to send an email through Powershell.  This is the powershell script that I came up with and tested.

function sendMyEmail ($fromAddress, $toAddress, $subject, $body, $password)
{
    # The sendEmail function is setup to use a GMail STMP Server with a valid account    
    $SMTPServer = "smtp.gmail.com"
    $SMTPClient = New-Object System.Net.Mail.SmtpClient
    $SMTPClient.Host = 'smtp.gmail.com'
    $SMTPClient.Port = 587
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($fromAddress, $password);
    $SMTPClient.Send($fromAddress, $toAddress, $subject, $body)
}

function gatherInfo {

}

#Main 
$smtpInfo = New-Object PSObject -Property @{
    fromAddress = $null
    toAddress = $null
    subject = $null
    body = $null
    password = $null
}
$smtpInfo.fromAddress = "myemail@gmail.com"
$smtpInfo.toAddress = "mystuff@scriptkitty.work"
$smtpInfo.subject = "Awesome Email"
$smtpInfo.body = "Email is Awesome"
$smtpInfo.password = "xxxxxxxxxxxxx"
gatherInfo
sendMyEmail -fromAddress $smtpInfo.fromAddress -toAddress $smtpInfo.toAddress -subject $smtpInfo.subject -body $smtpInfo.body -password $smtpInfo.password

No comments:

Post a Comment

Docker with Juiceshop - Focus on SQL Injection

In preparation for an ethical hacking class that I will be teaching, I wanted to work through a few of the Vulnhub or docker images to refr...