2016-08-19 110 views
0

以下是我的VBScript代码,用于发送带有附加文件的电子邮件。该文件存在于我发送电子邮件时需要抓取的位置。从命令行传递文件位置

Set objMessage = CreateObject("CDO.Message") 
Set args = WScript.Arguments 
Set arg1 = args.Item(0) 
objMessage.Subject = "Sample subject" 
objMessage.From = "[email protected]" 
objMessage.To = "[email protected]" 
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 runtime error: Object required: '[string: "D:\Users\me\Desk"]'

任何建议可能是什么问题呢?

+0

你应该可以使用'objMessage.AddAttachment args(0)' – Squashman

+0

我试过你说的,但它给了我相同的确切的错误。 – JohnG

+0

这很奇怪。这就是我用vbscript完成所有电子邮件的方式。我使用了一个通用的vbscript,我把所有的东西都作为参数传递给它。 – Squashman

回答

1

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

更改此:

Set arg1 = args.Item(0) 

到这一点:

arg1 = args.Item(0) 

和错误将消失。