Email without activeX control

Author Message
bigwill

  • Total Posts : 12
  • Scores: 0
  • Reward points : 0
  • Joined: 8/22/2006
  • Status: offline
Email without activeX control Friday, November 03, 2006 6:55 AM (permalink)
0
As has been pointed out - there are probably better ways to do this.

Object oriented VBScript to send an email using plink.exe :

 'Send_EMail_Using_Plink.vbs
 'Written by : Big Will
 '2006.11.02
 '
 'Based on tribina's original idea for a simple email sub that I found at
 'http://www.windowsitpro.com/Article/ArticleID/46746/46746.html
 '
 'TEMailClass uses PuTTY Link: command-line connection utility
 'http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
 'You could easily use NetCat or any other telnet client that accepts
 'input from stdIn, but NOT microsoft's Telnet.exe :(
 '
 'Written entirely in Notepad++ (hint: tabstops set to 2)
 'http://notepad-plus.sourceforge.net/uk/site.htm
 
 option explicit
 
 class TEmailBodyClass
 'This Class provides functionality to the Email class below for
 'reading and writing the text of the body.
 'It's simply a wrapper for an array.
 
 private fBodyLineArray
 
 private sub Class_Initialize(  )
   fBodyLineArray = Array( )
   redim fBodyLineArray(0) 'maybe a bit redundant, but I was getting
                            'some strange results without that line.
 end sub
 
 public function LineCount()
   'return line count - read only
   LineCount = uBound(fBodyLineArray) + 1 
 end function
 
 public function GetText()
   'get contents of fBodyLineArray - read only
   dim i
   dim sTemp
   
   for i = lBound(fBodyLineArray) to uBound(fBodyLineArray)
     sTemp = sTemp & fBodyLineArray(i) & VbCrLf
   next
   GetText = sTemp
   'GetText = Join(fBodyLineArray) 'quicker, easier, but I want them to come 
   'back the way I wrote it :P
 end function
 
 public sub Addline(sLine)
   'adds (appends) an individual line to the end of the Body array. 
   dim size   'there is a gotcha here - if you send in a string with CrLf
   dim sTemp  'in the middle, this is going to come out strange.
   'sTemp = Replace(sLine, VbCrLf, "")
   sTemp = sLine
   size = uBound(fBodyLineArray)
   if (size = 0) and (fBodyLineArray(0) = "") then
     fBodyLineArray(0) = sTemp
   else
     redim preserve fBodyLineArray(size + 1)
     fBodyLineArray(uBound(fBodyLineArray)) = sTemp
     'wscript.echo "uBound after redim : " & uBound(fBodyLineArray)
   end if
 end sub
 
 public sub SetText(aArray)
   dim i
   dim sTemp
   dim ArrayTemp
   dim offset
   dim s1, s2
   
   ArrayTemp = Array( )
   redim ArrayTemp(0)
   offset = 0
   if not IsArray(aArray) then  'check to see if we got a string, or an array
     sTemp = aArray
     aArray = Split(sTemp, chr(13)) 'turn it into an array if sent the contents of a file
   end if
   for i = lBound(aArray) to uBound(aArray)  'we must clean up the 
     if (InStr(aArray(i), vbLF) = 1) then    'extra LF chars after 
       if (Len(aArray(i)) = 1) then          'the split.
         offset = offset + 1                  'skip it if all we got was a LF
       else
         aArray(i) = Replace(aArray(i), vbLF, "")
         redim preserve ArrayTemp(i - offset)
         ArrayTemp(i - offset) = aArray(i)
       end if
     else
       redim preserve ArrayTemp(i - offset) 'else we were sent an array
       ArrayTemp(i - offset) = aArray(i)     'and we can just copy it
     end if    
     'wscript.echo i - offset & " : " & fBodyLineArray(i - offset) & " : " & len(fBodyLineArray(i - offset)) _
     '  & " : " & i
   next
   for i = lBound(ArrayTemp) to uBound(ArrayTemp)
     AddLine ArrayTemp(i)
   next 
 end sub
 
 public function LoadFromFile(sFileName)
   Const ForReading = 1
   Const ForWriting = 2
   Const ForAppending = 8
   
   dim FSO
   dim sContents
   dim oFile
   
   set FSO = CreateObject("Scripting.FileSystemObject")
   set oFile = FSO.OpenTextFile(sFileName, ForReading)
   on error resume next
   sContents = oFile.ReadAll
   if err.number <> 0 then
     LoadFromFile = err.number
   else
     SetText sContents
   end if
   oFile.Close
 end function
 
 public function ClearText()
   'clears the fBodyLineArray - destructive
   Redim fBodyLineArray(0)
   fBodyLineArray(0) = ""
 end function
 
 end class
 
 '------------------------------------------------------------------
 
 class TEmailClass
 
 private fEmailMsgArr
 private fHeaderTo
 private fHeaderFrom
 private fBodySubject
 private fBodyDate
 private fBodyFrom
 private fBodyTo
 private fBodySenderName
 private fEmailServer
 public Body
 
 private sub Class_Initialize(  )
   'create the "body" when we create a new instance of this class
   set Body = new TEmailBodyClass
 end sub
 
 private sub Class_Terminate(  )
   'destroy the body when the class is destroyed
   set Body = nothing
 end sub
 
 '     { let }
 
 public property let EmailServer(aEmailServer)
   'Sets the email server we are going to connect to
   '*REQUIRED*
   fEmailServer = aEmailServer
 end property
 
 public property let DestAddress(aToAddress)
   'Sets the Destination (to:) email address
   '*REQUIRED*
   fHeaderTo = aToAddress
 end property
 
 public property let FromAddress(aFromAddress)
   'Sets the return (from:) address
   'also interpreted as the return address
   '*REQUIRED*
   fHeaderFrom = aFromAddress
 end property
 
 public property let Subject(aSubject)
   'Sets the Subject line in the email. 
   'Not actually a header, as it is included after the "DATA" command
   'is sent to the email server.
   'not required
   fBodySubject = aSubject
 end property
 
 public property let RecipientName(aRecipientName)
   'This is a text field, and represents the Textual "name" of the person
   'you are sending the email to.
   'not required
   fBodyTo = aRecipientName
 end property
 
 public property let SenderName(aSenderName)
   'String Data representing the sender name
   'not required
   fBodySenderName = aSenderName
 end property
 
 '     { Get }
 'read assignments for the properties
 public property get EmailServer()
   EmailServer = fEmailServer
 end property
 
 public property get DestAddress()
   DestAddress = fHeaderTo
 end property
 
 public property get FromAddress()
   FromAddress = fHeaderFrom
 end property
 
 public property get Subject()
   Subject = fBodySubject
 end property
 
 public property get RecipientName()
   RecipientName = fBodyTo
 end property
 
 public property get SenderName()
   SenderName = fBodySenderName
 end property
 
 public sub SendEmailMsg
   dim smtpServerCommands
   dim oShell
   dim oExec
   dim smtpCommand
   'Construct an array with the data in it.  No checking, so if you
   'call SendEmailMsg without the fields filled out, then nothing's
   'going to happen, and you arent going to notice it.
   'this is NOT RFC compliant, so it may not work properly with all
   'email servers.
   smtpServerCommands = Array( _
     "HELO ", _
     "MAIL FROM:<" & fHeaderFrom & ">", _
     "RCPT TO:<" & fHeaderTo & ">", _
     "DATA", _
     "TO:" & fBodyTo, _
     "SUBJECT:" & fBodySubject , _
     "Date:" & Now, _
     "From:" & fBodySenderName, _
     "", _
     Body.GetText, _
     ".", _
     "quit")
   set oShell = CreateObject("wscript.Shell")
   'make a raw connection to the server on port 25 (email) 
   set oExec = oShell.Exec("plink.exe -raw " & fEmailServer & " -P 25")
   'execute and pause for a second so that the program can load and connect
   WScript.Sleep 1000
   'then we step though the array, sending one line at a time with a 
   'short delay. 
   for each smtpCommand in smtpServerCommands
     'write each line in the ServerCommands array to 
     'plink.exe, which is listening on StdIn with a pause so that we dont
     'drop any data in the pipe (rare, but could happen)
     oExec.StdIn.Write smtpCommand & VbCrLf
     WScript.sleep 25
   next
   
 end sub
 
 end class
 
 '-----------------------------------------------------------------------
 
 dim MyEmail
 set MyEmail = new TEmailClass
   MyEmail.EmailServer = "mail.destinationserver.com"
   MyEmail.DestAddress = "recipient@destinationserver.com"
   MyEmail.FromAddress = "sender@originationDomain.com"
   MyEmail.Subject = "Testing the Email class"
   MyEmail.RecipientName = "Big Will"
   MyEmail.SenderName = "Test Name"
   MyEmail.Body.AddLine "This was sent from" & vbCrLf
   MyEmail.Body.AddLine "my new VBScript(rev 1) at " & now 
   wscript.echo MyEmail.Body.GetText
   MyEmail.Body.LoadFromFile "C:\temp\scripts\send_email_using_plink.vbs"
   wscript.echo MyEmail.Body.GetText
   myEmail.SendEmailMsg
 


This could be modified to work with blat, or CDO or whatever you please. I wrote this before I was aware that BLAT existed.

-Big Will
<message edited by bigwill on Friday, November 10, 2006 9:47 AM>
 
#1
    DiGiTAL.SkReAM

    • Total Posts : 1259
    • Scores: 7
    • Reward points : 0
    • Joined: 9/7/2005
    • Location: Clearwater, FL, USA
    • Status: offline
    RE: Email without activeX control Friday, November 03, 2006 7:18 AM (permalink)
    0
    I am afraid that I have to admit that I am sorely not understanding the logic here. 
    You don't want to worry about having an ActiveX control or Exchange on a system.
         but
    You WANT to be sure that you have plink.exe on a system?
     
    I don't see the benefit over using CDO.  I utilize CDO on all of the XP and 2003 servers that I manage to send email, and on NT 4.0 servers, I use blat.
    CDO is nice and fast, and easy to utilize.  Oh, and it is a part of the OS, always a plus.
     
    "Would you like to touch my monkey?" - Dieter (Mike Meyers)

    "It is better to die like a tiger, than to live like a pussy."
    -Master Wong, from Balls of Fury
     
    #2
      bigwill

      • Total Posts : 12
      • Scores: 0
      • Reward points : 0
      • Joined: 8/22/2006
      • Status: offline
      RE: Email without activeX control Friday, November 10, 2006 9:41 AM (permalink)
      0
      I showed this script to a friend, and he convinced me to repost it.

      Thanks
      -Big Will
       
      #3

        Online Bookmarks Sharing: Share/Bookmark

        Jump to:

        Current active users

        There are 0 members and 1 guests.

        Icon Legend and Permission

        • New Messages
        • No New Messages
        • Hot Topic w/ New Messages
        • Hot Topic w/o New Messages
        • Locked w/ New Messages
        • Locked w/o New Messages
        • Read Message
        • Post New Thread
        • Reply to message
        • Post New Poll
        • Submit Vote
        • Post reward post
        • Delete my own posts
        • Delete my own threads
        • Rate post

        2000-2012 ASPPlayground.NET Forum Version 3.9