VBScript,在没有SMTP服务器的情况下发送电子邮件



我正在尝试创建一个VBS脚本,该脚本在文件夹达到特定文件大小时发送警报电子邮件,但我似乎无法让它发送电子邮件。我收到此错误 - "传输无法连接到服务器"。有没有办法在没有SMTP服务器的情况下发送电子邮件?

出于 obv 原因,我更改了我的 pswrd/电子邮件。

Const dirPath     = "C:Userstim.mcgeeDesktopOffsite 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

错误消息表示脚本无法连接到端口 25 上的smtp.gmail.com。如今,作为垃圾邮件预防措施,大多数ISP不允许端口25上的出站邮件。您需要通过他们的一个邮件中继发送邮件,或者远程服务器必须接受不同端口上的邮件,通常是 587(提交)或 465(SMTPS,已弃用)。

由于您已经拥有凭据,因此您可能应该将 SMTPPort 的值更改为 587 或 465。Gmail 应接受上述任一端口上的经过身份验证的邮件。

至于您在没有SMTP服务器的情况下发送邮件的问题,使用CDO时,您基本上有3个发送消息的选项。您可以通过sendusing配置字段选择要使用的一个:

  • cdoSendUsingPickup(数值 1 ):允许您发送邮件而无需指定 SMTP 服务器,但您必须在运行脚本的主机上安装 SMTP 服务器。使用此方法,邮件通过分拣文件夹提交到本地SMTP服务器。不需要身份验证,但必须将 SMTP 服务器配置为正确路由邮件。

    通常,当您设置本地 SMTP 服务器时,这些服务器配置为将所有已拾取的邮件发送到中央邮件网关/集线器,该网关/集线器处理进一步的传递。

  • cdoSendUsingPort(数值2,默认值):允许您通过SMTP协议将邮件发送到任何SMTP服务器。还允许您提供显式凭据。使用此方法时,必须指定要将邮件发送到的 SMTP 服务器。

  • cdoSendUsingExchange(数值 3):允许您通过域的 Exchange 服务器发送邮件。显然需要一个域和一个 Exchange 服务器。

相关内容

最新更新