VBS script and Batch script needed to send gmail email

I would like to see a sample VBS and Batch script that will send from a gmail account using port 587, with an attachment.
100questionsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
This will work on the Batch (it's a powershell and can be used on the Tasks scheduller)

<#
$username = 'jortega928@gmail.com'
$password = 'xxx'
 

$smtp = new-object Net.Mail.SmtpClient('smtp.gmail.com') 
$smtp.EnableSsl = $true 
$msg = new-object Net.Mail.MailMessage  

$msg.From = $username
$msg.To.Add("jortega@faboit.com") 
$msg.BodyEncoding = [system.Text.Encoding]::Unicode 
$msg.SubjectEncoding = [system.Text.Encoding]::Unicode 
$msg.IsBodyHTML = $true  
$msg.Subject = "File is Attached" 
$msg.Body = "The file is attached"
$msg.Attachments.Add("Z:\WORKING_DIR\file1.csv")

try{
$SMTP.Credentials = New-Object System.Net.NetworkCredential("$username", "$password"); 
$smtp.Send($msg)
}
catch{
    $er = $_.Exception.Message
    Write-Error $_.Exception.Message
}


#>

try{
    $From = "jortega928@gmail.com"
    $To = "jortega@faboit.com"
    $Cc = "ceo@faboit.com"
    $Attachment = "Z:\WORKING_DIR\file1.csv"
    $Subject = "Email Subject"
    $Body = "Insert body text here"
    $SMTPServer = "smtp.gmail.com"
    $SMTPPort = "587"
    Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
    -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
    -Credential (Get-Credential -UserName $From -Message "Insert the password for $From") -Attachments $Attachment
}
catch{
    Write-Error $_.Exception.Message
}

Open in new window


Here's 2 ways, one commented and one without comments

Here's how to configure a Ps1 in a Task Scheduler.
https://social.technet.microsoft.com/wiki/contents/articles/38580.configure-to-run-a-powershell-script-into-task-scheduler.aspx

you just need to go you here: https://myaccount.google.com/lesssecureapps
and Enabled the "LessSecureApps" to Enable, so you can use the script.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
In VBS we typically do this using CDO.  But CDO does not support TLS (587), only SSL (465), so that is typically what is used for sending via GMail SMTP server.  If you are interested in a 465 VBS example let me know and I will post...

From a BAT script BLAT is often the tool of choice...  http://www.blat.net/


»bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.