1

I'm trying to create a VBS script that sends an alert email when a folder has reached a specific file size, but I can't seem to get it to send an email. I get this error - "The transport failed to connect to the server". Is there any way to send an email without a SMTP server or?

I changed my pswrd/email an stuff for obv reasons.

Const dirPath     = "C:\Users\tim.mcgee\Desktop\Offsite Drive"
Const alertedPath = "prevRun.txt"
      alertOn     = 3 * 2 ^ 29 '1.5GB
      resetOn     = alertOn * .95 'Approx 77MB
Const emailTo     = "**"
Const emailFrom   = "**"
Const emailSbjct  = "Offsite Drive Full"
Const emailMsg    = "The offsite drive has reached maximum capacity."
Const SMTPServer  = "Smtp.gmail.com"
Const SMTPPort    = 25
      emailUsr    = emailFrom 
Const emailPsswd  = "**"
Const emailSSL    = False


Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(alertedPath) Then
  alerted =  CBool(Trim(fso.OpenTextFile(alertedPath).ReadLine))
Else
  alerted = False
End If
dirSize = fso.GetFolder(dirPath).Size

If alerted Then 'Email previously sent
  alerted = dirSize > resetOn
ElseIf dirSize >= alertOn Then
  SendEmail
  alerted = True
End If

fso.OpenTextFile(alertedPath, 2, True).WriteLine CInt(alerted)
WScript.Quit 0

Sub SendEmail
  Const cfg = "http://schemas.microsoft.com/cdo/configuration/"
  With CreateObject("CDO.Message")
    .From                                  = emailFrom
    .To                                    = emailTo
    .Subject                               = emailSbjct
    .TextBody                              = emailMsg
    With .Configuration.Fields
      .Item(cfg & "sendusing")             = 2
      .Item(cfg & "smtpserver")            = SMTPServer
      .Item(cfg & "smtpserverport")        = SMTPPort
      .Item(cfg & "smtpconnectiontimeout") = 60
      .Item(cfg & "smtpauthenticate")      = 1
      .Item(cfg & "smtpusessl")            = emailSSL
      .Item(cfg & "sendusername")          = emailUsr
      .Item(cfg & "sendpassword")          = emailPsswd
      .Update
    End With
    .Send
  End With
End Sub
Khakis7
  • 413
  • 4
  • 19
  • Have you looked at http://stackoverflow.com/questions/28605803/can-not-send-mail-using-smtp-gmail-com-port-587-from-vbs-script? – Tony Hinkle Feb 24 '16 at 18:29

1 Answers1

0

The error message means that your script can't connect to smtp.gmail.com on port 25. Nowadays most ISPs don't allow outbound mail on port 25 as a spam prevention measure. You need to send either through one of their mail relays, or the remote server must accept mail on a different port, usually 587 (submission) or 465 (SMTPS, deprecated).

Since you already have credentials you should probably just change the value of SMTPPort to 587 or 465. Gmail should accept authenticated mail on either of those ports.

As for your question about sending mail without an SMTP server, when using CDO you basically have 3 options for sending messages. You select the one you want to use via the sendusing configuration field:

  • cdoSendUsingPickup (numeric value 1): allows you to send mail without having to specify an SMTP server, but you must have an SMTP server installed on the host where the script is running. With this method the mail is submitted to the local SMTP server via a pickup folder. Does not require authentication, but the SMTP server must be configured to route mail correctly.

    Normally, when you have a setup with local SMTP servers, these are configured to send all picked up mail to a central mail gateway/hub, which handles further delivery.

  • cdoSendUsingPort (numeric value 2, default): allows you to send mail to any SMTP server via the SMTP protocol. Also allows you to provide explicit credentials. With this method you must specify an SMTP server to send the mail to.

  • cdoSendUsingExchange (numeric value 3): allows you to send mail through the Exchange server of your domain. Requires a domain and an Exchange server, obviously.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328