Avatar of E=mc2
E=mc2Flag for Canada

asked on 

Send email using VBSCRIPT with attachment

I would like to send an email in an Exchange 2010 environment using VBSCRIPT.
The email would need to send an attachment to a specific email address when I run the script.
For the attachment it would look inside a folder and if a file exists in the folder, then it needs to send out an email and attach the file.  The filename could differ at any given time.  Only should the script send out an email if a file exists in the specific folder it's looking inside.  If no file exists, then no email is sent out.
Microsoft Legacy OSWindows OSMicrosoft Server OS

Avatar of undefined
Last Comment
Steve Knight
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this.  Haven't tested it yet...

Steve

Const MailServer = "12.34.56.78" ' Mail Server to use for SMTP
Const Sender = "logs@somedomain.com" 
Const FilePath="D:\test"
Const Subject="Subject line"
Const Message="Please add the message to add in body here"
Const Recipient="logs@somedomain.com"

set objFSO = CreateObject("Scripting.FileSystemObject")
set objFolder=objFSO.GetFolder(FilePath)

Set objEmail = CreateObject("CDO.Message")

count=0
For Each f In objFolder.Files
  objEmail.AddAttachment f.name
  count=count+1
Next

if count=0 then
  msgbox "No files found"
  wscript.quit
end if

objEmail.From = Sender
objEmail.To = Recipient
objEmail.Subject = Subject
objEmail.Textbody = Message

objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = MailServer
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update()
objEmail.Send

Open in new window

Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Just need to change

objEmail.AddAttachment f.name

to

objEmail.AddAttachment f.path

to use the full file path.  I had my test file in same dir as script.

Steve
Avatar of E=mc2
E=mc2
Flag of Canada image

ASKER

@dragon-it -- The file I want to attach is does not have a static name but it changes often.  What is the same is the extension, say .txt
Would the file path command Const FilePath="D:\test", is this the location where the file that I want to attach is found?
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Yes, as it stands it picks up any file in the d,test directory and attaches them to the email.

if you have a different criteria say and we can build that in
Avatar of E=mc2
E=mc2
Flag of Canada image

ASKER

@dragon-it --
When you list the "bjEmail.Configuration.Fields.Item" lines, are these somehow pulling from the internet?
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

No, though it looks like it that it just how you make the settings!
Avatar of SubSun
SubSun
Flag of India image

If you are interested, You can use PowerShell.. You can send mail using Send-MailMessage command..

Ref : http://technet.microsoft.com/en-us/library/hh849925.aspx
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

You mention text files, if there are other files in the dir. too then you can filter those out with something like:

For Each f In objFolder.Files
  if lcase(right(f.name,4))=".txt" then
      objEmail.AddAttachment f.name
      count=count+1
  end if
Next

Likewise as I said can soon have it move the file to a different directory after it is attached or rename it etc. too.

Steve
Avatar of E=mc2
E=mc2
Flag of Canada image

ASKER

@dragon-it -- The script does not seem to have an area where you can put in a password for the email server.
Is it possible to modify it so that a password can be added?
Avatar of E=mc2
E=mc2
Flag of Canada image

ASKER

@subsun -- Thanks, however PowerShell does not work in my environment.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

we can add authentication to the smtp if you wish, will post from pc later, though that means incorporating password into the script.

is this running on a server, or user workstation where you could maybe send using outlook instead?
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of E=mc2
E=mc2
Flag of Canada image

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for 100questions's comment #a39501250

for the following reason:

Due to circumstances, the server was not allowing me to use PowerShell, therefore any potential reader of the comments made in answer to my question, need to try it out on their system first.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

You asked for VBScript not Powershell and we gave you VBScript?!!???
Avatar of E=mc2
E=mc2
Flag of Canada image

ASKER

I am not sure if this works.  Thanks for the information.  Any readers of this post should may need to try to see if the above solution works for them.  I am closing this so as to not keep it open, with the hope that this works.
Avatar of E=mc2
E=mc2
Flag of Canada image

ASKER

I am not sure if this works.  Thanks for the information.  Any readers of this post should may need to try to see if the above solution works for them.  I am closing this so as to not keep it open, with the hope that this works.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Well thanks for finishing it off, always a good idea when you ask a question and then someone gives you a specific answer to that question that you actually at least try it out, would take, let me see about 20 seconds to copy and paste into a text editor and run.

But then it looks like you asked for powershell and batch at the same time and deleted / B graded them too on account of not having time to test them...

Thanks for the "b" grade then.

Steve
Windows OS
Windows OS

This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.

129K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo