In my previous post, I described how to connect to a network drive.
In this post, I will demonstrate how to disconnect from an existing connected network drive.
Suppose we have a drive Z: that is a network drive connected. Following is the code regarding how ti remove it.
Option Explicit
Dim WshNetwork, ShareName
Set WshNetwork = WScript.CreateObject("WScript.Network")
ShareName = "Z:"
WshNetwork.RemoveNetworkDrive ShareName, true, true
WScript.Quit
We are creating an object of WScript.Network and calling method RemoveNetworkDrive by passing the drive letter.
This blog is dedicated to new technologies of Microsoft, including LINQ, sharepoint, ADO.NET entity framework and lot more
add
Showing posts with label VBScript. Show all posts
Showing posts with label VBScript. Show all posts
Tuesday, September 8, 2009
How to map network drive using VBScript
Some time it is required to map a network drive using vbscript.
Today I will demonstate how to connet and then disconnect from a network drive.
In order to connect to a network drive, we need following data/information:
1. HomeServer Location: Actual path of the drive. In our example it is
"\\125.99.218.158\Data"
2. Name of the drive you wish to give: Let us say Z:
3. UserId/Password using which we will connect the map drive. If userid is same as logged in user then it is not required.
We will create an object of WScript.Network and call its method MapNetworkDrive
Here is the code:
Option Explicit
Dim strUser, strPassword, strDriveLetter, strHomeServer, strProfile
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
strDriveLetter = "Z:"
strHomeServer = "\\125.99.218.158\Data"
strProfile = "False" ' Mapping (not) stored in user Profile
strUser = "USERID"
strPassword = "PASSWORD"
objNetwork.MapNetworkDrive strDriveLetter,strHomeServer,strProfile,strUser,strPassword
WScript.Quit
So the above code is creating an object of Wscript.Network and calling method MapNetworkDrive by passwing parameters like drive letter, hom server, userid and password.
There is one more parameter that is strProfile. If set true, it will store the drive info in user profile and it will connect automatically next time when user logs in.
Today I will demonstate how to connet and then disconnect from a network drive.
In order to connect to a network drive, we need following data/information:
1. HomeServer Location: Actual path of the drive. In our example it is
"\\125.99.218.158\Data"
2. Name of the drive you wish to give: Let us say Z:
3. UserId/Password using which we will connect the map drive. If userid is same as logged in user then it is not required.
We will create an object of WScript.Network and call its method MapNetworkDrive
Here is the code:
Option Explicit
Dim strUser, strPassword, strDriveLetter, strHomeServer, strProfile
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
strDriveLetter = "Z:"
strHomeServer = "\\125.99.218.158\Data"
strProfile = "False" ' Mapping (not) stored in user Profile
strUser = "USERID"
strPassword = "PASSWORD"
objNetwork.MapNetworkDrive strDriveLetter,strHomeServer,strProfile,strUser,strPassword
WScript.Quit
So the above code is creating an object of Wscript.Network and calling method MapNetworkDrive by passwing parameters like drive letter, hom server, userid and password.
There is one more parameter that is strProfile. If set true, it will store the drive info in user profile and it will connect automatically next time when user logs in.
Tuesday, July 21, 2009
How to send mail using CDO in VBScript
Today we will see how to send mail using CDO in VBScript. The same code can be used in ASP, VB also with small modification.
STEP 1: Define the constants:
Const cdoSendUsingMethod="http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort = 2
Const cdoSMTPServer ="http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort ="http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout ="http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic = 1
Const cdoSendUserName="http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword ="http://schemas.microsoft.com/cdo/configuration/sendpassword"
Const CdoReferenceTypeName = 1
STEP 2: Declare variables
Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields
Dim HtmlBody
STEP 3: Get a handle on the config object and it's fields
' Get a handle on the config object and it's fields
Set objConfig = CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
' Set config fields we care about
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = <SMTP-SERVER-NAME>
.Item(cdoSMTPServerPort) = <PORT-NUMBER>
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic
.Item(cdoSendUsingMethod ) = 2
.Update
End With
STEP 4: Create instance CDO.Messsage object
Set objMessage = CreateObject("CDO.Message")
STEP 5: Add image to the mail body
Add this step only if you have an image to add to the body
Set objBP = objMessage.AddRelatedBodyPart("C:\Users\Varun.Sharma\Shell\1.jpg", "1.jpg", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<1.jpg>"
objBP.Fields.Update
For description of step 5, visit: How to add image to mail body using CDO
STEP 6: assign the properties of CDO.Message object
Set objMessage.Configuration = objConfig
STEP 7: Build the HTML Body
Always prepare your html body before hand. That will help your code look better
HtmlBody="<img src=1.jpg><br>"
HtmlBody=HtmlBody & " Sending mail using CDO"
STEP 8: Set the CDO.Message mail properties
With objMessage
.To = To-Address
.Cc = CC-Address
.From = From-Address
.Subject = "SMTP Relay Test"
.HtmlBody=HtmlBody
.Send
End With
STEP 9: Release the CDO instances
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
FULL CODE:
Const cdoSendUsingMethod ="http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort = 2
Const cdoSMTPServer ="http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort ="http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout ="http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate ="http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic = 1
Const cdoSendUserName ="http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword ="http://schemas.microsoft.com/cdo/configuration/sendpassword"
Const CdoReferenceTypeName = 1
Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields
Dim HtmlBody
' Get a handle on the config object and it's fields
Set objConfig = CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
' Set config fields we care about
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = SMTP-SERVER-NAME
.Item(cdoSMTPServerPort) = PORT-NUMBER
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic
.Item(cdoSendUsingMethod ) = 2
.Update
End With
Set objMessage = CreateObject("CDO.Message")
Set objBP = objMessage.AddRelatedBodyPart(PHYSICAL-PATH-OF-IMAGE, "1.jpg", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<1.jpg>"
objBP.Fields.Update
Set objMessage.Configuration = objConfig
HtmlBody="<img src=1.jpg><br>"
HtmlBody=HtmlBody & "Testing the mail"
With objMessage
.To = TO-ADDRESS
.Cc = CC-ADDRESS
.From = FROM-ADDRESS
.Subject = "SMTP Relay Test"
'.TextBody = "SMTP Relay Test Sent @ " & Now()
.HtmlBody=HtmlBody
.Send
End With
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
STEP 1: Define the constants:
Const cdoSendUsingMethod="http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort = 2
Const cdoSMTPServer ="http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort ="http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout ="http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic = 1
Const cdoSendUserName="http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword ="http://schemas.microsoft.com/cdo/configuration/sendpassword"
Const CdoReferenceTypeName = 1
STEP 2: Declare variables
Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields
Dim HtmlBody
STEP 3: Get a handle on the config object and it's fields
' Get a handle on the config object and it's fields
Set objConfig = CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
' Set config fields we care about
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = <SMTP-SERVER-NAME>
.Item(cdoSMTPServerPort) = <PORT-NUMBER>
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic
.Item(cdoSendUsingMethod ) = 2
.Update
End With
STEP 4: Create instance CDO.Messsage object
Set objMessage = CreateObject("CDO.Message")
STEP 5: Add image to the mail body
Add this step only if you have an image to add to the body
Set objBP = objMessage.AddRelatedBodyPart("C:\Users\Varun.Sharma\Shell\1.jpg", "1.jpg", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<1.jpg>"
objBP.Fields.Update
For description of step 5, visit: How to add image to mail body using CDO
STEP 6: assign the properties of CDO.Message object
Set objMessage.Configuration = objConfig
STEP 7: Build the HTML Body
Always prepare your html body before hand. That will help your code look better
HtmlBody="<img src=1.jpg><br>"
HtmlBody=HtmlBody & " Sending mail using CDO"
STEP 8: Set the CDO.Message mail properties
With objMessage
.To = To-Address
.Cc = CC-Address
.From = From-Address
.Subject = "SMTP Relay Test"
.HtmlBody=HtmlBody
.Send
End With
STEP 9: Release the CDO instances
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
FULL CODE:
Const cdoSendUsingMethod ="http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort = 2
Const cdoSMTPServer ="http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort ="http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout ="http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate ="http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic = 1
Const cdoSendUserName ="http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword ="http://schemas.microsoft.com/cdo/configuration/sendpassword"
Const CdoReferenceTypeName = 1
Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields
Dim HtmlBody
' Get a handle on the config object and it's fields
Set objConfig = CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
' Set config fields we care about
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = SMTP-SERVER-NAME
.Item(cdoSMTPServerPort) = PORT-NUMBER
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic
.Item(cdoSendUsingMethod ) = 2
.Update
End With
Set objMessage = CreateObject("CDO.Message")
Set objBP = objMessage.AddRelatedBodyPart(PHYSICAL-PATH-OF-IMAGE, "1.jpg", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<1.jpg>"
objBP.Fields.Update
Set objMessage.Configuration = objConfig
HtmlBody="<img src=1.jpg><br>"
HtmlBody=HtmlBody & "Testing the mail"
With objMessage
.To = TO-ADDRESS
.Cc = CC-ADDRESS
.From = FROM-ADDRESS
.Subject = "SMTP Relay Test"
'.TextBody = "SMTP Relay Test Sent @ " & Now()
.HtmlBody=HtmlBody
.Send
End With
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
How to add image to mail body using CDO
Many times we face situation like, we need to send image in the html body of the mail that too using CDO.
Suppose we have an image at local server from where MailSend Code will get fire, here is the code:
Add this line of code to your CDO code to send image as a mail body:
Set objMessage = CreateObject("CDO.Message")
Set objBP = objMessage.AddRelatedBodyPart("C:\Users\Varun.Sharma\Shell\1.jpg", "1.jpg", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<1.jpg>"
objBP.Fields.Update
Here, in AddRelatedBodyPart,
argument 1 is : physical location of image
argument 2 is : identifier of the image (any name)
argument 3 is : To tell CDO to send the image as well
In next line, just put image identifer to the right hand side.
When sending mail refer the image in html body like this:
mail.HtmlBody="<img src=1.jpg" >
For full code of sending mail using CDO, check the following link:
How to send mail using cdo in vbscript
Suppose we have an image at local server from where MailSend Code will get fire, here is the code:
Add this line of code to your CDO code to send image as a mail body:
Set objMessage = CreateObject("CDO.Message")
Set objBP = objMessage.AddRelatedBodyPart("C:\Users\Varun.Sharma\Shell\1.jpg", "1.jpg", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<1.jpg>"
objBP.Fields.Update
Here, in AddRelatedBodyPart,
argument 1 is : physical location of image
argument 2 is : identifier of the image (any name)
argument 3 is : To tell CDO to send the image as well
In next line, just put image identifer to the right hand side.
When sending mail refer the image in html body like this:
mail.HtmlBody="<img src=1.jpg" >
For full code of sending mail using CDO, check the following link:
How to send mail using cdo in vbscript
Subscribe to:
Comments (Atom)