【问题标题】:Text Emailing using VBscript使用 VBscript 发送文本电子邮件
【发布时间】:2014-10-20 05:59:55
【问题描述】:

我已经尝试了以下代码,没有抛出错误。也没有发送任何电子邮件。

Dim i, objEmail

' Use custom error handling
On Error Resume Next

Set objEmail = CreateObject( "CDO.Message" )

' Fill in the field values
With objEmail
    .From     = "xyz@abc.com"
    .To       = "pqr@abc.com"
    .Subject  = "Test"
    .TextBody = "Email from VB Script" 

If mySMTPPort = "" Then
        mySMTPPort = 25
    End If

With .Configuration.Fields
        .Item( "http://schemas.microsoft.com/cdo/configuration/sendusing"      ) = 2
        .Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver"     ) = "HostNameHere"
        .Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ) = 25
        .Update
    End With
    .Send
End With
' Return status message
If Err Then
    EMail = "ERROR " & Err.Number & ": " & Err.Description
    Err.Clear
Else
    EMail = "Message sent ok"
End If

' Release the e-mail message object
Set objEmail = Nothing

当我执行这个 VBS 文件时,它什么也不做。请帮帮我。我必须将简单的文本电子邮件从我的域发送到另一个域。或者我想要执行的任务是否有解决方法?

【问题讨论】:

标签: email vbscript


【解决方案1】:

您在尝试发送邮件时可能会遇到错误。但是,您启用了错误处理 (On Error Resume Next),但您的错误处理程序只是将错误信息放入变量中,而实际上并未对该变量执行任何操作。添加与变量相呼应的行应该为您提供有关正在发生的事情的更多信息:

If Err Then
    EMail = "ERROR " & Err.Number & ": " & Err.Description
    Err.Clear
Else
    EMail = "Message sent ok"
End If
WScript.Echo EMail

应该为您提供有关正在发生的事情的更多信息。

编辑:显然您遇到了连接错误。有几个可能的原因。首先检查您的名称解析是否正常工作:

nslookup HostNameHere

如果名称无法解析,请使用脚本中的 IP 地址或修复名称解析。

接下来检查您是否可以通过名称和 IP 地址连接到远程主机上的端口 25:

telnet HostNameHere 25
telnet a.b.c.d 25

如果你得到这样的结果:

C:\>telnet HostNameHere 25
Connecting To HostNameHere...Could not open connection to the host, on port 25:
Connect failed

有些东西阻止了您的连接。可能是网络防火墙、远程主机上基于主机的防火墙,或者是服务一开始就没有监听端口 25。请咨询您的网络管理员和/或邮件服务器的管理员。

【讨论】:

  • 你是对的,现在我收到一个错误“传输连接失败”,你能帮忙吗?
【解决方案2】:

您的代码隐藏了错误。错误默认属性是数字。所以你说 if err.number = 0 (same as false) then error, if err.number not 0 (ie error) 一切正常。

【讨论】:

  • 错了。发生错误时,即填充 Err 对象时,会正确触发条件 If Err Then
猜你喜欢
  • 1970-01-01
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-10
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多