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.
100questionsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
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

Steve KnightIT ConsultancyCommented:
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
100questionsAuthor Commented:
@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?
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Steve KnightIT ConsultancyCommented:
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
100questionsAuthor Commented:
@dragon-it --
When you list the "bjEmail.Configuration.Fields.Item" lines, are these somehow pulling from the internet?
Steve KnightIT ConsultancyCommented:
No, though it looks like it that it just how you make the settings!
Subash SundharanIT Infrastructure Architect Commented:
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
Steve KnightIT ConsultancyCommented:
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
100questionsAuthor Commented:
@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?
100questionsAuthor Commented:
@subsun -- Thanks, however PowerShell does not work in my environment.
Steve KnightIT ConsultancyCommented:
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?
Steve KnightIT ConsultancyCommented:
Adding this should authenticate as the current user

objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 2

Or can make that =1 and use

objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusename") = "username"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"

Steve

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
100questionsAuthor Commented:
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.
Steve KnightIT ConsultancyCommented:
You asked for VBScript not Powershell and we gave you VBScript?!!???
100questionsAuthor Commented:
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.
100questionsAuthor Commented:
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.
Steve KnightIT ConsultancyCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.