Send email from VBScript using SMTP and Gmail

Today I wanted to send an email using SMTP from my gmail account. I searched the web and I found this and this and this which helped me a lot.
So this is the vbscript:

on error resume next

Const schema   = "http://schemas.microsoft.com/cdo/configuration/"
Const cdoBasic = 1
Const cdoSendUsingPort = 2
Dim oMsg, oConf

' E-mail properties
Set oMsg      = CreateObject("CDO.Message")
oMsg.From     = "from@gmail.com"  ' or "Sender Name <from@gmail.com>"
oMsg.To       = "test@test.gr"    ' or "Recipient Name <to@gmail.com>"
oMsg.Subject  = "Test from VBScript"
oMsg.TextBody = "If you can read this, the script worked!"

' GMail SMTP server configuration and authentication info
Set oConf = oMsg.Configuration
oConf.Fields(schema & "smtpserver")       = "smtp.gmail.com" 'server address
oConf.Fields(schema & "smtpserverport")   = 465              'port number
oConf.Fields(schema & "sendusing")        = cdoSendUsingPort
oConf.Fields(schema & "smtpauthenticate") = cdoBasic         'authentication type
oConf.Fields(schema & "smtpusessl")       = True             'use SSL encryption
oConf.Fields(schema & "sendusername")     = "from@gmail.com" 'sender username
oConf.Fields(schema & "sendpassword")     = "passwordi"      'sender password
oConf.Fields.Update()

' send message
oMsg.Send()

' Return status message
If Err Then
	resultMessage = "ERROR " & Err.Number & ": " & Err.Description
	Err.Clear()
Else
	resultMessage = "Message sent ok"
End If

Wscript.echo(resultMessage)
Advertisements

About cmanios

programming
This entry was posted in Networking, smtp and tagged , , , . Bookmark the permalink.

11 Responses to Send email from VBScript using SMTP and Gmail

  1. kevin says:

    nice. finaly a working one.

  2. Schuimtas says:

    error 0x80040217 : Server not available

  3. Sathish says:

    Works Fine.. Thanks for the script

  4. boss says:

    WORK :))

  5. Pravin Bankar says:

    Thanks .. It works for me… Thanks alot

  6. Deepa says:

    Hi
    This works fine. Thanks..
    Can you pls let me know how to send email with a file attachment?

    • CMA says:

      Just add the following after “oMsg.TextBody = “If you can read this, the script worked!””

      oMsg.AddAttachment “C:\path_to_file\file.xxx”

      • Pradeep says:

        I added the same but unable to run script after that. without attachment it’s working fine.

  7. faraz says:

    Excellent,,,,,,,,,,,, Works like a charm,,,,,,,,,,,,,
    Thank you very very much,,,,,, God bless you all the time.

  8. claudinei says:

    Working… very very thanks.
    In my case i need change the smtpusessl for false (configuration of my server email).

    Sorry for errors of ortography and concordance, i am brazilian and not talk english.

  9. Bhaskar says:

    Thank you very much. Very useful script. I added attachment, it is working fine.

    oMsg.AddAttachment Replace( myAttachment, “\”, “\\” ),””,””

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