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

8/22/2022 - Mon
Steve Knight

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 Knight

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
ASKER
E=mc2

@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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Steve Knight

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
ASKER
E=mc2

@dragon-it --
When you list the "bjEmail.Configuration.Fields.Item" lines, are these somehow pulling from the internet?
Steve Knight

No, though it looks like it that it just how you make the settings!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Subash Sundharan

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 Knight

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
ASKER
E=mc2

@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?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
E=mc2

@subsun -- Thanks, however PowerShell does not work in my environment.
Steve Knight

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
Steve Knight

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
E=mc2

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Steve Knight

You asked for VBScript not Powershell and we gave you VBScript?!!???
ASKER
E=mc2

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.
ASKER
E=mc2

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Steve Knight

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