home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / vbccmail / smtpsend.bas < prev    next >
Encoding:
BASIC Source File  |  1998-01-16  |  28.7 KB  |  1,067 lines

  1. Attribute VB_Name = "smtpsend"
  2. Option Explicit
  3.  
  4.  
  5.  
  6.  
  7. ' stuff for smtp client
  8.   Dim linenumber As Long
  9.  
  10. Dim BSMTPfiles() As String
  11. Dim NoofBSMTPfiles As Integer
  12. Dim CurrentBSMTPfile As Integer
  13. Dim ImACTIVE As Boolean
  14. Dim Timeout As Integer
  15. Dim tryingtoconnect As Boolean
  16. Dim SENDfilefh As Integer
  17. Dim SENDDATAeofflag As Boolean
  18. Dim sendingfile As Boolean
  19. Dim lastCommand As String
  20. Dim returnerror As String
  21. Dim failedaddresses As Integer
  22. Dim addressfailurelist As String
  23. Dim imconnected As Boolean
  24.  
  25. Global MAXto As Integer
  26.  
  27. Global ADDRESSto() As String
  28. Global ADDRESSfrom As String
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. Dim SendingDomain As String ' from helo
  36. Dim cTO As Integer
  37. Public sendmailtimer As Integer
  38. Const MYINTERNETSERVER = "192.168.0.101"
  39. Const SMTPTCPPORT = 25 ''' assigned TCP contact port */
  40. Const SMTPGREET = "220"  '' SMTP successful greeting */
  41. Const SMTPOK = "250" '' SMTP OK code */
  42. Const SMTPREADY = "354"  '' SMTP ready for data */
  43. Const SMTPSOFTFATAL = "421" '' SMTP soft fatal code */
  44. Const SMTPHARDERROR = "554" '' SMTP miscellaneous hard failure */
  45. Const OUR_DOMAIN = "ntuucp.envision-design.com.hk"
  46. 'stuff for uucp
  47.  
  48. Private Type rcptaddr
  49.   address As String
  50.   addrtype As String
  51.   name As String
  52.   domain As String
  53. End Type
  54. Dim sMailFrom As String
  55. Dim errorexist As Boolean
  56. Dim sRcpt() As rcptaddr
  57. Dim iRcptCnt As Integer
  58. Const WAITING_FOR_ADDRESSES = 1
  59. Const WAITING_FOR_MESSAGE = 2
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. 'Sub InitializeClient()
  69. '  sendingfile = False
  70. '  ImACTIVE = False
  71. '  SENDfilefh = -1
  72. '  sendmailtimer = mailserver.SENDMAILINTERVAL
  73. 'End Sub
  74.  
  75.  
  76.  
  77.  
  78. 'Sub BSMTPstartup()
  79. '  Dim tempfilename As String
  80. '  ReDim BSMTPfiles(1)
  81. '  Form1.List1.AddItem "Checking the outgoing files"
  82. '  NoofBSMTPfiles = 0
  83. '  tempfilename = Dir(mailserver.DEF_NET_DIR & "*.*")
  84. '  If tempfilename = "" Then
  85. '    ImACTIVE = False
  86. '    Form1.List1.AddItem "No Files to process - Closing"
  87. '    Exit Sub
  88. '  End If
  89. '  BSMTPfiles(1) = mailserver.DEF_NET_DIR & tempfilename
  90. '  NoofBSMTPfiles = 1
  91. '  tempfilename = Dir
  92. '  Do While Not tempfilename = ""
  93. '    NoofBSMTPfiles = NoofBSMTPfiles + 1
  94. '    ReDim Preserve BSMTPfiles(NoofBSMTPfiles)
  95. '    BSMTPfiles(NoofBSMTPfiles) = mailserver.DEF_NET_DIR & tempfilename
  96. '    tempfilename = Dir
  97. '  Loop
  98. '  CurrentBSMTPfile = 1
  99. '  doPARSE
  100. 'End Sub
  101.  
  102.  
  103.  
  104. 'Sub doPARSE() ' reads a bsmtp file and turns it into a smtp out file
  105. '
  106. '  Dim words() As String
  107. '  Dim nowords As Integer
  108. '  Dim buf As String
  109. '
  110. '  Form1.List1.AddItem "Parsing " & CurrentBSMTPfile
  111. 'open_file:
  112. '  On Error GoTo failtoopen
  113. '  SENDfilefh = FreeFile
  114. '  Open BSMTPfiles(CurrentBSMTPfile) For Input As #SENDfilefh
  115. '  On Error GoTo 0
  116. '
  117. 'init_state:
  118. '  If EOF(SENDfilefh) Then
  119. '    GoTo seen_QUIT
  120. '  End If
  121. '  Line Input #SENDfilefh, buf
  122. '  nowords = split(buf, words)
  123. '
  124. '  If nowords = 0 Then
  125. '    GoTo init_state ''NOT VERY FORGIVING!
  126. '  End If
  127. '  Select Case UCase(words(1))
  128. '    Case "HELO"
  129. '      If nowords > 1 Then
  130. '        SendingDomain = words(2)
  131. '      Else
  132. '        SendingDomain = OUR_DOMAIN
  133. '      End If
  134. '      MAXto = 0
  135. '      ReDim ADDRESSto(1)
  136. '      GoTo seen_HELO
  137. '    Case Else
  138. '      GoTo init_state
  139. '  End Select
  140. '
  141. 'seen_HELO:
  142. '  If EOF(SENDfilefh) Then
  143. '    GoTo seen_QUIT
  144. '  End If
  145. '  Line Input #SENDfilefh, buf
  146. '  nowords = split(buf, words)
  147. '
  148. '  If nowords = 0 Then
  149. '    GoTo seen_HELO
  150. '  End If
  151. '  Select Case UCase(words(1))
  152. '    Case "MAIL"
  153. '    Form1.List1.AddItem "MAIL: w2=" & words(2)
  154. '      If Trim(UCase(words(2))) = "FROM:" Then
  155. '        Form1.List1.AddItem "MAIL: w3=" & words(3)
  156. '        ADDRESSfrom = copypath(words(3))
  157. '        GoTo seen_MAIL
  158. '      Else
  159. '        ADDRESSfrom = copypath(Mid(words(2), 6))
  160. '        GoTo seen_MAIL
  161. '      End If
  162.  
  163. '    Case "RSET", "QUIT" 'if i get quit somethings gone wrong!
  164. '      GoTo seen_RSET
  165. '    Case Else
  166. '      Form1.List1.AddItem "OOOPS, GOT " & buf
  167. '      GoTo seen_HELO
  168. '  End Select
  169. '
  170. 'seen_MAIL:
  171.  
  172. '  If EOF(SENDfilefh) Then
  173. '    GoTo seen_QUIT
  174. '  End If
  175. '  Line Input #SENDfilefh, buf
  176. '
  177. '  nowords = split(buf, words)
  178. '  If nowords = 0 Then
  179. '    GoTo seen_HELO
  180. '  End If
  181. '  Select Case UCase(words(1))
  182. '    Case "RCPT"
  183. 'Form1.List1.AddItem "TO: w2=" & words(2)
  184. '   If Trim(UCase(words(2))) = "TO:" Then
  185. '   Form1.List1.AddItem "TO: w3=" & words(3)
  186. '
  187. '        addpath words(3)
  188. '        GoTo seen_MAIL
  189. '      Else
  190. '        addpath Mid(words(2), 4)
  191. '        GoTo seen_MAIL
  192. '      End If
  193. '    Case "DATA"
  194. '      GoTo seen_DATA
  195. '    Case "RSET"
  196. '      GoTo seen_RSET
  197. '    Case "MAIL"
  198. '      GoTo seen_MAIL
  199. '    Case "QUIT"
  200. '      GoTo seen_RSET
  201. '    Case Else
  202. '      GoTo seen_MAIL
  203. '  End Select
  204. '
  205. 'seen_RSET:
  206. '
  207. '  MAXto = 0
  208. '  ReDim ADDRESSto(1)
  209. '  ADDRESSfrom = ""
  210. '  GoTo seen_HELO
  211. '
  212. 'seen_DATA:
  213. '  Form1.List1.AddItem "Got data - processing!"
  214. '
  215. '  domail
  216. '  Exit Sub
  217. '
  218. 'seen_QUIT:
  219. '
  220.   ' got eof before data!, kill the file and go to next file!
  221. '  Close #SENDfilefh
  222. '  Kill BSMTPfiles(CurrentBSMTPfile)
  223. 'failtoopen:
  224. '  CurrentBSMTPfile = CurrentBSMTPfile + 1
  225. '  If CurrentBSMTPfile > NoofBSMTPfiles Then
  226. '    'there are no more files to read
  227. '    ImACTIVE = False
  228. '    Exit Sub
  229. '  End If
  230. '  MAXto = 0
  231. '  ReDim ADDRESSto(1)
  232. '  ADDRESSfrom = ""
  233. '  GoTo open_file
  234. '
  235. 'End Sub
  236.  
  237.  
  238.  
  239. Sub domail()
  240.   If imconnected Then
  241.     doRSET
  242.     Exit Sub
  243.   Else
  244.     doINIT
  245.     Exit Sub
  246.   End If
  247. End Sub
  248.  
  249.  
  250.  
  251. ' all routines must check sendmailactive before entering!!!
  252. Sub doINIT()
  253.   Dim test As Variant
  254.   lastCommand = "INIT"
  255.  Form1.List1.AddItem "Connecting to server", 0
  256. ' Form1.tcps.Disconnect
  257.   tryingtoconnect = True
  258.   Timeout = 10
  259.   Form1.tcps.LocalPort = 221
  260.   Form1.tcps.HostAddr = "192.168.0.101"
  261.   Form1.tcps.HostPort = 25
  262.   Form1.tcps.LocalPort = 0
  263.   Form1.tcps.Connect
  264. '  Form1.tcps.LocalPort = 221
  265. '  Form1.tcps.Connect
  266.   SENDfilefh = FreeFile
  267.   Open CCMAILWORKINGFOLDER & "SMTPOUT" & ".txt" For Input As #SENDfilefh
  268.   ''debug.print Form1.tcps.Send(vbCrLf)
  269.   ' for now I'm going to assume this will work!!
  270. End Sub
  271.  
  272.  
  273. Private Sub SMTPclientTIMER_Timer()
  274.  Dim test As Variant
  275.   Dim ontime As Boolean
  276.   Dim ontoday As Boolean
  277.   Dim numfirstcalltime As Integer
  278.   Dim numlastcalltime As Integer
  279.   Dim numnowtime As Integer
  280.   If Form1.List1.ListCount > 20 Then
  281.     Form1.List1.RemoveItem 0
  282.   End If
  283.   
  284.   If ImACTIVE Then
  285.     Select Case Timeout
  286.       Case Is > 0
  287.         Timeout = Timeout - 1
  288.       Case 0
  289.         ' if trying to connect then do a dial and
  290.         If tryingtoconnect Then
  291.           tryingtoconnect = False
  292.           Form1.List1.AddItem "Trying to start RAS", 0
  293.           test = Shell("C:\winnt\system32\RASPHONE.exe -D SUPERNET", 1)
  294.           Timeout = -1
  295.           sendmailtimer = 25 ' will try again in 25 seconds
  296.         End If
  297.         'debug.print "Closing "; Form1.tcps.Close
  298.         If SENDfilefh > 0 Then
  299.           Close #SENDfilefh
  300.         End If
  301.         ImACTIVE = False
  302.         Exit Sub
  303.     End Select
  304.     'Socket errors
  305.     Me.Caption = "Timeout in " & Timeout & " seconds "
  306.    ' Select Case Form1.tcps.state
  307.   '    Case 0, 8 ' Closed, Closed by server, error
  308.  '       Form1.List1.AddItem "Closed by server or error"
  309. '        If SENDfilefh > 0 Then'
  310.           'Close #SENDfilefh
  311. '        End If'
  312. '        ImACTIVE = False
  313. '        Exit Sub
  314. '      Case 9
  315. '        Form1.List1.AddItem "Closed by me??"
  316. '        Form1.tcps.Close
  317. '        If SENDfilefh > 0 Then
  318. '          Close #SENDfilefh
  319. '        End If
  320. '        ImACTIVE = False
  321. '        Exit Sub
  322. '    End Select
  323.   Else
  324.     
  325.     ' check call times and days
  326.     ' if it is off set sendmailtimer to -1
  327.  
  328.   ' MyDate represents a Wednesday.
  329.     If UCase(Mid(mailserver.calldays, WeekDay(Now), 1)) = Mid(mailserver.calldays, WeekDay(Now), 1) Then
  330.        ontoday = True
  331.     Else
  332.        ontoday = False
  333.     End If
  334.     numfirstcalltime = Val(Left(mailserver.firstcalltime, InStr(mailserver.firstcalltime, ":") - 1)) * 60 + Val(Mid(mailserver.firstcalltime, InStr(mailserver.firstcalltime, ":") + 1))
  335.     numlastcalltime = Val(Left(mailserver.lastcalltime, InStr(mailserver.lastcalltime, ":") - 1)) * 60 + Val(Mid(mailserver.lastcalltime, InStr(mailserver.lastcalltime, ":") + 1))
  336.     numnowtime = Val(Format(Now, "hh")) * 60 + Val(Format(Now, "nn"))
  337.     If numnowtime > numfirstcalltime And numnowtime < numlastcalltime Then
  338.       ontime = True
  339.     Else
  340.       ontime = False
  341.     End If
  342.     
  343.     If sendmailtimer > 5 And (Not ontime Or Not ontoday) Then
  344.       sendmailtimer = -1
  345.        mailserver.cbtimer.Caption = "OFF"
  346.      mailserver.cbtimer.value = 0
  347.       Me.Caption = "SMTP Client Off"
  348.  
  349.     Else
  350.       If sendmailtimer < 0 And ontime And ontoday Then
  351.         sendmailtimer = mailserver.SENDMAILINTERVAL
  352.       mailserver.cbtimer.Caption = "ON"
  353.       mailserver.cbtimer.value = 1
  354.  
  355.  
  356.       End If
  357.     End If
  358.     Select Case sendmailtimer
  359.       Case Is > 0
  360.         sendmailtimer = sendmailtimer - 1
  361.         Me.Caption = "Sending Mail in " & sendmailtimer & " seconds"
  362.       Case 0
  363.       
  364.         sendmailtimer = mailserver.SENDMAILINTERVAL
  365.         ImACTIVE = True
  366.         Checkuucpdir ' not event driven
  367.  '       Redirectmail
  368.   
  369.  
  370.  
  371.  If mailserver.cbtimer.value = 1 Then
  372.         BSMTPstartup ' event driven
  373.   End If
  374.     End Select
  375.   End If
  376. End Sub
  377.  
  378.  
  379.  
  380. 'smtp incomming - used by incomming data and
  381. 'intermitant utils to process all commands....
  382.  
  383. Sub SMTP_incoming(reply As String)
  384.  
  385.   Form1.List1.AddItem "S:" & reply, 0
  386.   Select Case lastCommand
  387.     Case "INIT"
  388.       If Not Left(reply, 3) = SMTPGREET Then
  389.         returnerror = reply
  390.         doQUIT
  391.         Exit Sub
  392.       End If
  393.       doHELO
  394.       Exit Sub
  395.     Case "HELO"
  396.       If Not Left(reply, 3) = SMTPOK Then
  397.         returnerror = reply
  398.         doQUIT
  399.         Exit Sub
  400.       End If
  401.       doRSET
  402.       Exit Sub
  403.     Case "RSET"
  404.       If Not Left(reply, 3) = SMTPOK Then
  405.         returnerror = reply
  406.         lastCommand = "QUIT"
  407.         doQUIT
  408.         Exit Sub
  409.       End If
  410.       doFROM
  411.       Exit Sub
  412.     Case "FROM"
  413.       If Not Left(reply, 3) = SMTPOK Then
  414.         doQUIT
  415.         Exit Sub
  416.       End If
  417.       'send the first address!
  418.       addressfailurelist = ""
  419.       failedaddresses = 0
  420.       cTO = 1
  421.       doTO 1
  422.       Exit Sub
  423.     Case "TO"
  424.       If Not Left(reply, 3) = SMTPOK Then
  425.       '*** will need to change this later???
  426.         addressfailurelist = reply & vbCrLf
  427.         failedaddresses = failedaddresses + 1
  428.       End If
  429.       cTO = cTO + 1
  430.       If Not cTO > MAXto Then
  431.         doTO cTO
  432.         Exit Sub
  433.       End If
  434.       If MAXto = failedaddresses Then
  435.         doNEXTfile
  436.         Exit Sub
  437.       End If
  438.       doDATA
  439.       Exit Sub
  440.  
  441.     Case "DATA"
  442.       If Not Left(reply, 3) = SMTPREADY Then 'IS this ok, ready to send
  443.         lastCommand = "QUIT"
  444.         doQUIT
  445.         Exit Sub
  446.       End If
  447.       doSENDDATA
  448.       Exit Sub
  449.  
  450.     Case "SENDDATA"
  451.       If Not Left(reply, 3) = SMTPOK Then
  452.       '*** IS THIS THE RETURN CODE FOR DATA SENT OK!
  453.         doQUIT
  454.         Exit Sub
  455.       End If
  456.       doNEXTfile
  457.       Exit Sub
  458.     Case "QUIT"
  459.       'this is where it closes the connection
  460.  
  461.      Form1.tcps.Disconnect
  462.       Form1.List1.AddItem "closed connection", 0
  463.       ImACTIVE = False
  464.     Case Else
  465.       MsgBox "OOPS COCKED UP SOMEWHERE"
  466.       End
  467.   End Select
  468.       
  469. End Sub
  470.       
  471.       
  472. Sub doNEXTfile()
  473.   If SENDfilefh > -1 Then
  474.     Close #SENDfilefh
  475.   End If
  476.   If failedaddresses > 0 Then
  477.     doSENDERROR
  478.     'add error message to users file!!
  479.   End If
  480.  
  481.   MAXto = 0
  482.   ReDim ADDRESSto(1)
  483.   ADDRESSfrom = ""
  484.   If CurrentBSMTPfile > NoofBSMTPfiles Then
  485.     'there are no more files to read
  486.     doQUIT
  487.     Exit Sub
  488.   End If
  489.   Form1.tcps.Disconnect
  490.   sendingmail = False
  491. End Sub
  492.       
  493. Sub doHELO()
  494.    lastCommand = "HELO"
  495.    
  496.    Form1.tcps.SendData "HELO " & OUR_DOMAIN & vbCrLf, Len("HELO " & OUR_DOMAIN) + 2
  497.      
  498.        Form1.List1.AddItem "C: HELO " & OUR_DOMAIN
  499. End Sub
  500.  
  501. Sub doRSET()
  502.    lastCommand = "RSET"
  503.       If Form1.tcps.Send("RSET" & vbCrLf) < 0 Then
  504.      'debug.print "error sending data"
  505.     End If
  506.  
  507.    Form1.List1.AddItem "C:RSET", 0
  508. End Sub
  509.  
  510. Sub doFROM()
  511.    lastCommand = "FROM"
  512.   If Form1.tcps.Send("MAIL FROM: <" & ADDRESSfrom & ">" & vbCrLf) < 0 Then
  513.      'debug.print "error sending data"
  514.   End If
  515.    Form1.List1.AddItem "C:MAIL FROM: <" & ADDRESSfrom & ">", 0
  516. End Sub
  517.  
  518. Sub doTO(i As Integer)
  519.   lastCommand = "TO"
  520. If Form1.tcps.Send("RCPT TO: <" & ADDRESSto(i) & ">" & vbCrLf) < 0 Then
  521.      'debug.print "error sending data"
  522.   End If
  523.    Form1.List1.AddItem "C:RCPT TO: <" & ADDRESSto(i) & ">", 0
  524. End Sub
  525.  
  526.  
  527. Sub doNOOP()
  528.   lastCommand = "NOOP"
  529.   If Form1.tcps.Send("NOOP" & vbCrLf) < 0 Then
  530.      'debug.print "error sending data"
  531.   End If
  532.   
  533.   Form1.List1.AddItem "C:NOOP", 0
  534. End Sub
  535.  
  536.  
  537.  
  538. Sub doDATA()
  539.   lastCommand = "DATA"
  540.   If Form1.tcps.Send("DATA" & vbCrLf) < 0 Then
  541.        'debug.print "error sending data"
  542.   End If
  543.  
  544.   Form1.List1.AddItem "C:DATA", 0
  545. End Sub
  546.  
  547.  
  548. Sub doQUIT()
  549.    lastCommand = "QUIT"
  550.    Form1.tcps.Send ("QUIT" & vbCrLf)
  551.   
  552.  
  553.    Form1.List1.AddItem "C:QUIT", 0
  554. End Sub
  555.  
  556. Sub doSENDDATA()
  557.   linenumber = 0
  558.   lastCommand = "SENDINGDATA"
  559. ' just add the header bit on and send out file until eof or "."
  560.   Dim buf As String
  561.   
  562.   If Form1.tcps.Send("Received: ") < 0 Then
  563.        'debug.print "error sending data"
  564.   End If
  565.  
  566.   If Not SendingDomain = "" Then
  567.     If Form1.tcps.Send("from " & SendingDomain & " ") < 0 Then
  568.            'debug.print "error sending data"
  569.   End If
  570.  
  571.   End If
  572.   If Not OUR_DOMAIN = "" Then
  573.     If Form1.tcps.Send("by " & OUR_DOMAIN & " ") < 0 Then
  574.            'debug.print "error sending data"
  575.   End If
  576.  
  577.   End If
  578.   If Not ADDRESSfrom = "" Then
  579.     If Form1.tcps.Send("for " & ADDRESSfrom & " ") < 0 Then
  580.          'debug.print "error sending data"
  581.   End If
  582.  
  583.   End If
  584.   If Form1.tcps.Send("with BSMTP basic(1.0);" & vbCrLf & "  " & Format(Now, "ddd, d mmm yy hh:nn:ss +08:00") & vbCrLf) < 0 Then
  585.          'debug.print "error sending data"
  586.   End If
  587.  
  588.  
  589.  
  590.   sendingfile = True
  591.   Do While sendingfile = True
  592.     sendaline
  593.     DoEvents
  594.   Loop
  595. End Sub
  596.   
  597. Sub sendaline()
  598.   Dim buf As String
  599.   
  600.   On Error GoTo SendDataeof
  601.   If EOF(SENDfilefh) Then
  602.     GoTo SendDataeof
  603.   End If
  604.   Line Input #SENDfilefh, buf
  605.   If buf = "." Then
  606.     GoTo SendDataeof
  607.   End If
  608.   If Form1.tcps.Send(buf & vbCrLf) < 0 Then
  609.            'debug.print "error sending data"
  610.   End If
  611.  
  612.   linenumber = linenumber + 1
  613.   Form1.Caption = "Sending line - " & linenumber
  614.   Exit Sub
  615.  
  616. SendDataeof:
  617.   On Error GoTo 0
  618.   Close #SENDfilefh
  619.   sendingfile = False
  620.   If Form1.tcps.Send("." & vbCrLf) < 0 Then
  621.          'debug.print "error sending data"
  622.   End If
  623.  
  624.   Form1.List1.AddItem "C: Message Sent", 0
  625.   lastCommand = "SENDDATA"
  626.   SENDDATAeofflag = True
  627.   SENDfilefh = -1
  628.   Timeout = 300
  629. End Sub
  630.  
  631. Sub doSENDERROR()
  632.   Dim errorfile As String
  633.   Dim errorfh As Integer
  634.   Dim buf As String
  635.   Dim username As String
  636.   username = Left(ADDRESSfrom, InStr(ADDRESSfrom, "@") - 1)
  637.   
  638.   If Dir(mailserver.DEF_POP3_DIR & username, vbDirectory) = "" Then
  639.     MkDir mailserver.DEF_POP3_DIR & username
  640.   End If
  641.   errorfh = FreeFile
  642.   errorfile = getnewfilename(mailserver.DEF_POP3_DIR & username)
  643.   Open errorfile For Output Lock Read Write As #errorfh
  644.   Print #errorfh, "HELO " & OUR_DOMAIN
  645.   Print #errorfh, "MAIL FROM: <>"
  646.   Print #errorfh, "RCPT TO: <" & ADDRESSfrom & ">"
  647.   Print #errorfh, "DATA"
  648.   Print #errorfh, "To: " & ADDRESSfrom
  649.   Print #errorfh, "From: postmaster@" & OUR_DOMAIN
  650.   Print #errorfh, "Subject: Error Sending Mail to Internet"
  651.   Print #errorfh, ""
  652.   Print #errorfh, "--------------------------------------------------"
  653.   Print #errorfh, "ERRORS"
  654.   Print #errorfh, "--------------------------------------------------"
  655.   Print #errorfh, addressfailurelist
  656.   Print #errorfh, "--------------------------------------------------"
  657.   Print #errorfh, "MESSAGE"
  658.   Print #errorfh, "--------------------------------------------------"
  659.   SENDfilefh = FreeFile
  660.   Open BSMTPfiles(CurrentBSMTPfile) For Input As #SENDfilefh
  661.   Do While Not EOF(SENDfilefh)
  662.     Line Input #SENDfilefh, buf
  663.     If UCase(Left(buf, 4)) = "DATA" Then
  664.       Exit Do
  665.     End If
  666.   Loop
  667.   Do While Not EOF(SENDfilefh)
  668.     Line Input #SENDfilefh, buf
  669.     Print #errorfh, buf
  670.   Loop
  671.   Close #SENDfilefh
  672.   Close #errorfh
  673.   
  674. End Sub
  675.  
  676.  
  677.  
  678. ''BSMTP CODE ---------------------------------------------------
  679.  
  680.  
  681. Function copypath(s As String) As String
  682.   Dim rval As String
  683.   If s = "" Then
  684.     copypath = ""
  685.     Exit Function
  686.   End If
  687.   
  688.   If InStr(s, "<") = 0 Then
  689.     rval = s
  690.   Else
  691.     rval = Mid(s, (InStr(s, "<") + 1))
  692.   End If
  693.   If InStr(rval, ">") > 0 Then
  694.     rval = Left(rval, (InStr(rval, ">") - 1))
  695.   End If
  696.   Form1.List1.AddItem "added address : " & rval, 0
  697.   copypath = rval
  698. End Function
  699.  
  700. Function split(s As String, ByRef strarray() As String) As Integer
  701.   Dim buf As String
  702.   Dim nvecs As Integer
  703.   Dim slen As Integer
  704.   Dim sp As Integer
  705.   Dim lastp As String
  706.   ReDim strarray(1)
  707.  
  708.   If s = "" Then
  709.     split = 0
  710.     Exit Function
  711.   End If
  712.   slen = Len(Trim(s))
  713.   buf = Trim(s)
  714.   nvecs = 0
  715.   lastp = ""
  716.   For sp = 1 To slen
  717.     If Mid(buf, sp, 1) = " " Then
  718.       nvecs = nvecs + 1
  719.       ReDim Preserve strarray(nvecs)
  720.       strarray(nvecs) = lastp
  721.       lastp = ""
  722.     End If
  723.     If Asc(Mid(buf, sp, 1)) > 31 Then
  724.       lastp = lastp & Mid(buf, sp, 1)
  725.     End If
  726.   Next
  727.   nvecs = nvecs + 1
  728.   ReDim Preserve strarray(nvecs)
  729.   strarray(nvecs) = lastp
  730.   split = nvecs
  731. End Function
  732.  
  733. Sub addpath(s As String)
  734.   If s = "" Then
  735.      Exit Sub
  736.   End If
  737.   MAXto = MAXto + 1
  738.   ReDim Preserve ADDRESSto(MAXto)
  739.   ADDRESSto(MAXto) = copypath(s)
  740. End Sub
  741.  
  742. Function getnewfilename(directoryname As String) As String
  743.   Dim testfilename As String
  744.   Dim testnumber As Integer
  745.   testnumber = 0
  746.   testfilename = directoryname & "\Vbmail" & Format(Now, "yyyymmddhhnnss")
  747.   Do While Not Dir(testfilename & testnumber) = ""
  748.     testnumber = testnumber + 1
  749.   Loop
  750.   getnewfilename = testfilename & testnumber
  751. End Function
  752.  
  753.  
  754.  
  755.  
  756. 'Sub Checkuucpdir()
  757. '  Dim Current_addressfile As String
  758. '
  759. '  Dim astring As String
  760. '  Dim currentstate As Integer
  761. '  Dim udirfh As Integer
  762. '  Dim xfilename As String
  763. '  Dim dfilename As String
  764. '  Dim i As Long
  765. '  Dim xfiles() As String
  766. '  ReDim xfiles(0)
  767. '  Form1.List1.AddItem "UUCP checking directory", 0
  768. '  DoEvents 'lets makesure its showing!
  769. '
  770.   ' this bit is here to stop continuous looping for failed files!
  771. '  xfilename = Dir(mailserver.UUCP_DIR & "\*.x??")
  772. '  Do While Not xfilename = ""
  773. '    ReDim Preserve xfiles(UBound(xfiles) + 1)
  774. '    xfiles(UBound(xfiles)) = xfilename
  775. '    xfilename = Dir
  776. '  Loop
  777. '
  778.   
  779. '  For i = 1 To UBound(xfiles)
  780. '    processuucpfiles xfiles(i)
  781. '  Next
  782.   'SHOULD NOT BE ANY NEED TO REMOVE THE D FILES - JUST LEAVE UM THERE!
  783.    
  784.   ' loop - move all d files left over to unknown
  785. '  Form1.List1.AddItem "found all the x files - cleaning up"
  786. '  dfilename = Dir(mailserver.UUCP_DIR & "\*.d??")
  787. '  Do While Not dfilename = ""
  788. '    If Dir(mailserver.UUCP_DIR & "\UNKNOWN", vbDirectory) = "" Then
  789. '      MkDir mailserver.UUCP_DIR & "\UNKNOWN"
  790. '    End If
  791. '    Name mailserver.UUCP_DIR & "\" & dfilename As mailserver.UUCP_DIR & "\UNKNOWN\" & dfilename
  792. '    dfilename = Dir(mailserver.UUCP_DIR & "\*.d??")
  793. '  Loop
  794. 'End Sub
  795.  
  796. 'Sub processuucpfiles(addrfile As String)
  797. '  Dim addfh As Integer, astring As String
  798. '  Dim dfilename As String
  799. '  Dim drealfilename As String
  800. '  Dim ioutfh() As Integer
  801. '  iRcptCnt = 0
  802. '  drealfilename = ""
  803. '  sMailFrom = ""
  804. '  addfh = FreeFile
  805. '  On Error GoTo cantopenfile
  806.  ' Open mailserver.UUCP_DIR & "\" & addrfile For Input As #addfh
  807. '  On Error GoTo uucpfoundeof
  808. '  Do While Not EOF(addfh)
  809. '    Line Input #addfh, astring
  810. '    Select Case UCase(Left(astring, 2))
  811. ''      Case "F " ' ASSUME THIS IS CORRECT
  812.  '       dfilename = Trim(Mid(astring, 12))
  813.  '     Case "R "
  814.  '       If InStr(astring, "!") = 0 Then
  815.  '         sMailFrom = ""
  816.  '       Else
  817.  '         'domain = first bit, address = last bit
  818. '          sMailFrom = Trim(Mid(astring, InStr(astring, "!") + 1)) & "@" & Left(Trim(Mid(astring, 3)), InStr(Mid(astring, 3), "!") - 1)'
  819. '        End If'
  820. '      Case "C "
  821. '        astring = Trim(Mid(astring, 9)) ' get rid of 'c rmail '
  822. '        Do While InStr(astring, " ") > 0
  823. '          iRcptCnt = iRcptCnt + 1
  824. '          ReDim Preserve sRcpt(iRcptCnt)
  825. '          sRcpt(iRcptCnt).address = Left(astring, InStr(astring, " ") - 1)
  826. ''          astring = Trim(Mid(astring, InStr(astring, " ") + 1))
  827.  '       Loop
  828.  '       iRcptCnt = iRcptCnt + 1
  829.  '       ReDim Preserve sRcpt(iRcptCnt)
  830.  '       sRcpt(iRcptCnt).address = astring
  831.  '   End Select
  832.  ' Loop
  833.   
  834. 'uucpfoundeof:
  835. '  Close #addfh
  836. '  Form1.List1.AddItem "Processing Mail from " & sMailFrom
  837. '
  838.   ' look file the dfilename
  839. '    drealfilename = Dir(mailserver.UUCP_DIR & "\" & dfilename & ".d??")
  840. '  If (Not drealfilename = "") And (Not sRcpt(1).address = "") Then ' must have a recipient & a file!
  841. '    Processmail drealfilename
  842. '    Kill mailserver.UUCP_DIR & "\" & drealfilename
  843. '    If Not Dir(mailserver.UUCP_DIR & "\" & addrfile) = "" Then
  844. '      Kill mailserver.UUCP_DIR & "\" & addrfile ' kill the x file!
  845. '    End If
  846. '  End If
  847. 'cantopenfile:
  848. 'End Sub
  849.   
  850. 'Sub Processmail(dfile As String)
  851. '  'Messagetypes CCMAIL, SYSTEM, ERROR
  852. '  Dim i As Integer, j As Integer
  853. '  Dim ccmailcheck As String
  854. '  Dim localexist   As Boolean
  855. '  Dim ccmailexist   As Boolean
  856. ''  Dim inetexist As Boolean
  857.  '
  858.  ' localexist = False
  859.  ' ccmailexist = False
  860.  ' inetexist = False
  861.  ' For i = 1 To iRcptCnt
  862.  '   sRcpt(i).domain = Trim(Mid(sRcpt(i).address, InStr(sRcpt(i).address, "@") + 1))
  863.  '   sRcpt(i).name = Left(sRcpt(i).address, InStr(sRcpt(i).address, "@") - 1)
  864.  '   sRcpt(i).addrtype = ""
  865.  '   ' email address = gjc.cgcs.ccmail@envision-design.com.hk
  866.  '   ' how are we going to deal with spaces? = convert them to extra dots!!
  867.  '   If LCase(Right(sRcpt(i).name, 7)) = ".ccmail" And LCase(sRcpt(i).domain) = LCase(OUR_DOMAIN) Then
  868.  '     sRcpt(i).addrtype = "CCMAIL"
  869.  '     ccmailexist = True
  870.  '   Else
  871.  '     If LCase(sRcpt(i).domain) = LCase(OUR_DOMAIN) Then
  872.  ''       ' check for mail redirection
  873.   '      For j = 1 To UBound(redirections)
  874.   '        If LCase(redirections(j).rdFrom) = LCase(sRcpt(i).name) Then
  875.   '           If InStr(redirections(j).rdTo, "@") > 0 Then
  876.   '                  sRcpt(i).domain = Trim(Mid(redirections(j).rdTo, InStr(redirections(j).rdTo, "@") + 1))
  877.   '                  sRcpt(i).name = Left(redirections(j).rdTo, InStr(redirections(j).rdTo, "@") - 1)
  878.   '                  sRcpt(i).addrtype = "INTERNET"
  879.   '                   inetexist = True
  880. '                    Exit For
  881.   '           Else
  882.   '                  If LCase(Right(redirections(j).rdTo, 7)) = ".ccmail" Then
  883.   '                        sRcpt(i).name = redirections(j).rdTo
  884.   '                        sRcpt(i).addrtype = "CCMAIL"
  885. '                          ccmailexist = True
  886.   '                        Exit For
  887.   '                  Else
  888.   '                    If LCase(redirections(j).rdTo) = "trash" Then
  889.   '                          sRcpt(i).name = redirections(j).rdTo
  890.   '                          sRcpt(i).addrtype = "TRASH"
  891. '                      Else
  892.   '                          sRcpt(i).name = redirections(j).rdTo
  893.   '                          sRcpt(i).addrtype = "LOCAL"
  894. '                             localexist = True
  895.   '                    End If
  896.   '                    Exit For
  897.   '               End If
  898. '              End If
  899.   '        End If
  900.   '      Next
  901. '        If sRcpt(i).addrtype = "" Then
  902.   '          sRcpt(i).addrtype = "LOCAL"
  903.   '          localexist = True
  904.   '      End If
  905.         
  906.   '   Else
  907.   '      sRcpt(i).addrtype = "INTERNET" ' need this cause off ccmail re-routing!
  908.   '      inetexist = True
  909. '     End If
  910.   '  End If
  911.   'Next
  912. '  For i = 1 To iRcptCnt
  913.   'Form1.List1.AddItem "Sending File to " & sRcpt(i).name & "-@-" & sRcpt(i).domain & " of type " & sRcpt(i).addrtype, 0
  914.   'Next
  915.   '
  916.   
  917.   'If ccmailexist Then
  918.   '  sendtoccmail dfile
  919.   'End If
  920. '  If localexist Then
  921.   '  sendtolocaluser dfile
  922.   'End If
  923. '  If inetexist Then
  924.   '  sendtoinet dfile
  925.   'End If
  926.   '
  927. '  Form1.List1.AddItem "finished sending message from :" & sMailFrom, 0
  928. 'E 'nd Sub
  929. '
  930. 'Sub sendtolocaluser(datafile As String)
  931. '  Dim iFH As Integer
  932. '  Dim oFH As Integer, astring As String, i As Integer, buf As String
  933. '  Dim newfilename As String
  934. '   ' if the users file exist the append, otherwise copy!
  935. '  For i = 1 To iRcptCnt
  936. '    If sRcpt(i).addrtype = "LOCAL" Then
  937. '''      Form1.List1.AddItem "adding message to user:" & sRcpt(i).name
  938.   '    If Dir(mailserver.DEF_POP3_DIR & sRcpt(i).name, vbDirectory) = "" Then 'users folder doesn't exist!
  939.  '       If Dir(mailserver.DEF_POP3_DIR & "UNKNOWN", vbDirectory) = "" Then
  940.  '         MkDir mailserver.DEF_POP3_DIR & "UNKNOWN"
  941.  '       End If
  942. ' 'for the moment we will dump all unknown mail into the unknown mailfolder
  943.   '      newfilename = getnewfilename(mailserver.DEF_POP3_DIR & "UNKNOWN")
  944.   '      Form1.List1.AddItem "could not find user " & sRcpt(i).name & " - adding to UNKNOWN", 0
  945.         
  946.         ' copy the data file as well
  947. '        sRcpt(I).addrtype = "ERROR : User " & sRcpt(I).name & " Not Known at this Post office"
  948.  '       errorexist = True
  949.   '    Else
  950.        ' check if to file exists, if it does rename this one!
  951.   '      newfilename = getnewfilename(mailserver.DEF_POP3_DIR & sRcpt(i).name)
  952.   '     Form1.List1.AddItem "Adding to userfiles " & newfilename
  953. '
  954. '      End If
  955.         ' must have data at start!!!!
  956.         ' no option here !!
  957. '      oFH = FreeFile
  958. '      Open newfilename For Output As #oFH
  959. '      iFH = FreeFile
  960. '      Open mailserver.UUCP_DIR & "\" & datafile For Input As #iFH
  961. ''      Print #oFH, "HELO"
  962.  '     Print #oFH, "MAIL FROM: <" & sMailFrom & ">"
  963.  '     Print #oFH, "RCPT TO: <" & sRcpt(i).name & "@" & sRcpt(i).domain & ">"
  964.  '     Print #oFH, "DATA"
  965.  '     On Error GoTo closethem
  966.  '     Do While Not EOF(iFH)
  967.  '       Line Input #iFH, buf
  968.  '       Print #oFH, buf
  969.  '     Loop
  970. 'closethem:
  971. '      On Error GoTo 0
  972. '      Close #iFH
  973. '      Close #oFH
  974. '      Form1.List1.AddItem "Message added to folder", 0
  975. '    End If
  976. '  Next
  977. 'End Sub
  978.  
  979.  
  980. 'Sub sendtoccmail(datafile As String)
  981. '  Dim iFH As Integer
  982. '  Dim oFH As Integer, astring As String, i As Integer, buf As String
  983. '  Dim newfilename As String
  984. '   ' if the users file exist the append, otherwise copy!
  985. '
  986. '  newfilename = getnewfilename(mailserver.ccMAILspoolDIR)
  987. '    Form1.List1.AddItem "Adding to ccmail  folder" & mailserver.ccMAILspoolDIR, 0
  988. '  oFH = FreeFile
  989. '  Open newfilename For Output As #oFH
  990. ''  iFH = FreeFile
  991.  ' Open mailserver.UUCP_DIR & "\" & datafile For Input As #iFH
  992.  ' Print #oFH, "HELO"
  993.  ' Print #oFH, "MAIL FROM: <" & sMailFrom & ">"
  994.  ' For i = 1 To iRcptCnt
  995.  '     If sRcpt(i).addrtype = "CCMAIL" Then
  996.  '       Print #oFH, "RCPT TO: <" & sRcpt(i).name & "@" & sRcpt(i).domain & ">"
  997.  '     End If
  998. '  Next
  999.  ' Print #oFH, "DATA"
  1000. '  On Error GoTo closethem
  1001. '  Do While Not EOF(iFH)
  1002. '     Line Input #iFH, buf
  1003. '     Print #oFH, buf
  1004. '  Loop
  1005. 'closethem:
  1006. '  On Error GoTo 0
  1007. '  Close #iFH
  1008. '  Close #oFH
  1009. '  Form1.List1.AddItem "Message added to folder", 0
  1010. '
  1011. '
  1012. 'End Sub
  1013.  
  1014.  
  1015. 'Sub sendtoinet(datafile As String)
  1016. '  Dim iFH As Integer
  1017. '  Dim oFH As Integer, astring As String, i As Integer, buf As String
  1018. '  Dim newfilename As String
  1019. '   ' if the users file exist the append, otherwise copy!
  1020. '
  1021. '   If Dir(mailserver.DEF_NET_DIR, vbDirectory) = "" Then 'net folderdoesn't exist!
  1022. '     If Dir(mailserver.DEF_POP3_DIR & "UNKNOWN", vbDirectory) = "" Then
  1023. '       MkDir mailserver.DEF_POP3_DIR & "UNKNOWN"
  1024. '     End If
  1025. '     newfilename = getnewfilename(mailserver.DEF_POP3_DIR & "UNKNOWN")
  1026. '     Form1.List1.AddItem "could not find net folder  - adding to UNKNOWN", 0
  1027. '  Else
  1028. '     newfilename = getnewfilename(mailserver.DEF_NET_DIR)
  1029. '    Form1.List1.AddItem "Adding to net folder" & mailserver.DEF_NET_DIR
  1030. '  End If
  1031. '  oFH = FreeFile
  1032. '  Open newfilename For Output As #oFH
  1033. '  iFH = FreeFile
  1034. '  Open mailserver.UUCP_DIR & "\" & datafile For Input As #iFH
  1035. '  Print #oFH, "HELO"
  1036. '  Print #oFH, "MAIL FROM:<" & sMailFrom & ">"
  1037. '  For i = 1 To iRcptCnt
  1038. '      If sRcpt(i).addrtype = "INTERNET" Then
  1039. '        Print #oFH, "RCPT TO:<" & sRcpt(i).name & "@" & sRcpt(i).domain & ">"
  1040. '      End If
  1041. '  Next
  1042. '  Print #oFH, "DATA"
  1043. '  On Error GoTo closethem
  1044. '  Do While Not EOF(iFH)
  1045. '     Line Input #iFH, buf
  1046. '     Print #oFH, buf
  1047. '  Loop
  1048. ''closethem:
  1049.  ' On Error GoTo 0
  1050.  ' Close #iFH
  1051.  ' Close #oFH
  1052.  ' Form1.List1.AddItem "Message added to folder"
  1053. '
  1054. 'End Sub
  1055.  
  1056.  
  1057.  
  1058. Private Sub tcpSMTPclient_DataReceived(ByVal data As String, ByVal l As Long)
  1059.   tryingtoconnect = False
  1060.   Timeout = -1
  1061.   Dim reply As String
  1062.   
  1063.   SMTP_incoming data
  1064. End Sub
  1065.  
  1066.  
  1067.