Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Email 在VBScript中指定windows文件夹路径_Email_Vbscript_Email Attachments_Cdo.message - Fatal编程技术网

Email 在VBScript中指定windows文件夹路径

Email 在VBScript中指定windows文件夹路径,email,vbscript,email-attachments,cdo.message,Email,Vbscript,Email Attachments,Cdo.message,我有一个vbscript,它将文件夹内容作为附件发送到我的电子邮件,但问题是我无法指定windows文件夹的路径,因为不同计算机的windows路径不同 在我的代码下面的作品 Const PATH=“C:\windows\Folder1\” 但由于路径对于不同的机器是不同的。我试着跟随,但没有成功 Const PATH=“%windows%\Folder1\” 下面是完整的vbscript代码 Const cdoSendUsingPickup = 1 'Send message using th

我有一个vbscript,它将文件夹内容作为附件发送到我的电子邮件,但问题是我无法指定windows文件夹的路径,因为不同计算机的windows路径不同

在我的代码下面的作品

Const PATH=“C:\windows\Folder1\”

但由于路径对于不同的机器是不同的。我试着跟随,但没有成功

Const PATH=“%windows%\Folder1\”

下面是完整的vbscript代码

Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).

Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM


Set objMessage = CreateObject("CDO.Message")
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFolder
Dim oFile
Dim oFiles
Const PATH = "%windows%\Folder\" 'This method not working!!!!! 
Set oFolder = fso.GetFolder(PATH)
Set oFiles= oFolder.files
objMessage.Subject = "This is the email subject"
objMessage.From = "mailSender@MyMail.com"
objMessage.To = ""
objMessage.TextBody = "This is the body of the email. I’m fairly unoriginal"
For Each oFile in oFolder.files
objMessage.AddAttachment PATH & oFile.name
Next
'==This section will provide the configuration information for the remote SMTP server.
'==End remote SMTP server configuration section==

objMessage.Send
当远程SMTP服务器的配置信息正确时,代码工作正常


我将如何在此脚本中指定windows、程序文件、桌面(特殊文件夹)?

由于windows安全体系结构,在您尝试时这样做不是一个好的做法。我将从SpecialDirectory课程开始:

更新:

示例用法:

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

'Your problem in a nutshell
'Const PATH = "c:\windows\system"  ' fails on systems with %windir% <> c:\windows
'Const PATH = "%windir%\system"  ' fails with "Path not found" (FSO does not expand env vars)

Dim goWS   : Set goWS = CreateObject("WScript.Shell")
' PATH can't be a Const, because Consts can be initialized with literals only
' I use the prefix "cs" to indicate "constant string - keep your fingers off!"
Dim csPATH  : csPATH   = goWS.ExpandEnvironmentStrings("%windir%\system")
Dim csDESKT : csDESKT  = goWS.SpecialFolders("desktop")
WScript.Echo "# of files in system folder:", goFS.GetFolder(csPATH).Files.Count
WScript.Echo "# of files in desktop:", goFS.GetFolder(csDESKT).Files.Count

如果您的目标是发送带有附件的电子邮件?我将使用以下示例:

Public Shared Function SendMail(strFrom As String, strTo As String, strSubject As String, strMsg As String) As Boolean
Try
    ' Create the mail message
    Dim objMailMsg As New MailMessage(strFrom, strTo)

    objMailMsg.BodyEncoding = Encoding.UTF8
    objMailMsg.Subject = strSubject
    objMailMsg.Body = strMsg
    Dim at As New Attachment(Server.MapPath("~/Uploaded/txt.doc"))
    objMailMsg.Attachments.Add(at)
    objMailMsg.Priority = MailPriority.High
    objMailMsg.IsBodyHtml = True

    'prepare to send mail via SMTP transport
    Dim objSMTPClient As New SmtpClient()
    objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis
    objSMTPClient.Send(objMailMsg)
    Return True
Catch ex As Exception
    Throw ex
End Try
端函数

如果要使用文件夹位置附加文件。首先,我不会使用c:\windows\folder1作为文件的位置。由于此文件夹包含所有您的/客户端系统文件,您可能会遇到安全问题

插入以下代码: 你的代码

\\ Const PATH = "%windows%\Folder\" 'This method not working!!!!! 
\\ Set oFolder = fso.GetFolder(PATH)
使用以下命令

string PATH = My.Computer.FileSystem.SpecialDirectories.MyDocuments 
返回字符串“C:\Users\Owner\Documents”。在这里,您可以在上面的代码中添加新文件夹。像这样使用连接&“\”和“Folder1”


希望这有帮助…

那么我的脚本应该是什么样子呢。对不起,我是vb脚本的初学者,请说明您的代码需要放在哪里??感谢您的回复。我感谢您的示例用法,您理解我,但正如我所说,我是一个初学者,我尝试将您的代码放在我的代码中,但每次都会出现错误,例如“预期fso”。请编辑我的代码并在您的回复中提及。提醒:我的脚本可以很好地将文件夹的内容附加到电子邮件中,但“windows、程序文件等”路径显然是个问题,因为它在不同的pc上会发生变化。认真地说,这将解决我数周来的挫折!!!谢谢我的脚本应该是什么样子。对不起,我是vb脚本的初学者,请说明代码需要放在哪里??该目录可以是所有窗口中的任何公用文件夹,例如程序文件、窗口、桌面等。您的意思是字符串路径?。这是一个帮助提交者理解逻辑的伪代码。在vb.net中,它将以字符串形式显示路径…我指的是您的vb.net代码;OT要求提供VBScript建议。
\\ Const PATH = "%windows%\Folder\" 'This method not working!!!!! 
\\ Set oFolder = fso.GetFolder(PATH)
string PATH = My.Computer.FileSystem.SpecialDirectories.MyDocuments