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?
 
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
0
 
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

0
 
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
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
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?
0
 
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
0
 
100questionsAuthor Commented:
@dragon-it --
When you list the "bjEmail.Configuration.Fields.Item" lines, are these somehow pulling from the internet?
0
 
Steve KnightIT ConsultancyCommented:
No, though it looks like it that it just how you make the settings!
0
 
SubsunCommented:
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
0
 
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
0
 
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?
0
 
100questionsAuthor Commented:
@subsun -- Thanks, however PowerShell does not work in my environment.
0
 
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?
0
 
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.
0
 
Steve KnightIT ConsultancyCommented:
You asked for VBScript not Powershell and we gave you VBScript?!!???
0
 
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.
0
 
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.
0
 
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.