【问题标题】:Passing the file location from command line从命令行传递文件位置
【发布时间】:2016-08-19 18:02:34
【问题描述】:

下面是我的 VBScript 代码,用于发送附有文件的电子邮件。该文件位于我发送电子邮件时需要抓取的位置。

Set objMessage = CreateObject("CDO.Message")
Set args = WScript.Arguments
Set arg1 = args.Item(0)
objMessage.Subject = "Sample subject" 
objMessage.From = "test@gmail.com" 
objMessage.To = "test2@gmail.com" 
objMessage.TextBody = "Please see the error logs attached with this email"
objMessage.AddAttachment ""&arg1&""
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "hostname"

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==

objMessage.Send

要通过命令行运行此脚本,我使用:

>cscript sendemail.vbs D:\users\me\Desktop\readme.txt

当我运行它时,我收到一条错误消息:

D:\Users\me\Desktop\sendemail.vbs(3, 1) Microsoft VBScript 运行时错误:需要对象:'[string: "D:\Users\me\Desk"]'

有什么建议吗?

【问题讨论】:

  • 你应该可以只使用objMessage.AddAttachment args(0)
  • 我试过你说的,但它给了我同样的错误。
  • 这很奇怪。这就是我使用 vbscript 处理所有电子邮件的方式。我使用了一个通用的 vbscript,我将所有内容都作为参数传递给它。

标签: command-line vbscript arguments email-attachments


【解决方案1】:

错误信息实际上说明了一切。虽然Arguments 集合是一个对象,但它的第一个元素不是。它是一个字符串,在 VBScript 中是一种原始数据类型。 Set 关键字专门用于将对象分配给变量。原始数据类型仅使用赋值运算符进行赋值。

改变这个:

Set arg1 = args.Item(0)

进入这个:

arg1 = args.Item(0)

错误就会消失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2019-11-03
    • 2014-11-05
    相关资源
    最近更新 更多