home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / sendmime / mimeform.frm (.txt) next >
Encoding:
Visual Basic Form  |  1999-08-28  |  21.7 KB  |  647 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    BackColor       =   &H00000000&
  4.    BorderStyle     =   4  'Festes Werkzeugfenster
  5.    Caption         =   "Sends E-Mail with Attachement!"
  6.    ClientHeight    =   5664
  7.    ClientLeft      =   1656
  8.    ClientTop       =   2208
  9.    ClientWidth     =   8184
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   5664
  14.    ScaleWidth      =   8184
  15.    StartUpPosition =   2  'Bildschirmmitte
  16.    Begin VB.CommandButton delattach 
  17.       Caption         =   "Del Attachement"
  18.       Height          =   375
  19.       Left            =   6120
  20.       TabIndex        =   7
  21.       Top             =   600
  22.       Width           =   1695
  23.    End
  24.    Begin VB.ListBox AttachementList 
  25.       Height          =   432
  26.       Left            =   4440
  27.       TabIndex        =   14
  28.       Top             =   120
  29.       Width           =   3375
  30.    End
  31.    Begin VB.CommandButton Exit 
  32.       BackColor       =   &H00808080&
  33.       Caption         =   "Exit"
  34.       Height          =   375
  35.       Left            =   4200
  36.       Style           =   1  'Grafisch
  37.       TabIndex        =   9
  38.       Top             =   5280
  39.       Width           =   3855
  40.    End
  41.    Begin VB.CommandButton SendMimeConnect 
  42.       Appearance      =   0  '2D
  43.       BackColor       =   &H00808080&
  44.       Caption         =   "Send"
  45.       Height          =   375
  46.       Left            =   120
  47.       Style           =   1  'Grafisch
  48.       TabIndex        =   8
  49.       Top             =   5280
  50.       Width           =   3975
  51.    End
  52.    Begin VB.ComboBox MailServer 
  53.       Appearance      =   0  '2D
  54.       BackColor       =   &H00FFFFFF&
  55.       Height          =   315
  56.       Left            =   720
  57.       TabIndex        =   1
  58.       Text            =   "mail.kdt.de"
  59.       Top             =   240
  60.       Width           =   2175
  61.    End
  62.    Begin VB.CommandButton Attachement 
  63.       BackColor       =   &H00000000&
  64.       Caption         =   "Add Attachement"
  65.       Height          =   375
  66.       Left            =   4440
  67.       TabIndex        =   6
  68.       Top             =   600
  69.       Width           =   1575
  70.    End
  71.    Begin VB.TextBox Tobox 
  72.       Appearance      =   0  '2D
  73.       BackColor       =   &H00FFFFFF&
  74.       Height          =   285
  75.       Left            =   720
  76.       MaxLength       =   50
  77.       TabIndex        =   2
  78.       Text            =   "galgen@wtal.de"
  79.       Top             =   720
  80.       Width           =   2175
  81.    End
  82.    Begin VB.ComboBox Frombox 
  83.       Appearance      =   0  '2D
  84.       BackColor       =   &H00FFFFFF&
  85.       Height          =   315
  86.       Left            =   720
  87.       TabIndex        =   3
  88.       Text            =   "me@host.com"
  89.       Top             =   1080
  90.       Width           =   2175
  91.    End
  92.    Begin VB.TextBox Subjekt 
  93.       Appearance      =   0  '2D
  94.       BackColor       =   &H00FFFFFF&
  95.       BeginProperty Font 
  96.          Name            =   "Courier"
  97.          Size            =   9.6
  98.          Charset         =   0
  99.          Weight          =   400
  100.          Underline       =   0   'False
  101.          Italic          =   0   'False
  102.          Strikethrough   =   0   'False
  103.       EndProperty
  104.       Height          =   285
  105.       Left            =   720
  106.       MaxLength       =   78
  107.       TabIndex        =   4
  108.       Top             =   1560
  109.       Width           =   7335
  110.    End
  111.    Begin VB.TextBox DataArrival 
  112.       Appearance      =   0  '2D
  113.       BackColor       =   &H00C0C0C0&
  114.       ForeColor       =   &H00000000&
  115.       Height          =   735
  116.       Left            =   120
  117.       MaxLength       =   1000
  118.       MultiLine       =   -1  'True
  119.       ScrollBars      =   2  'Vertikal
  120.       TabIndex        =   0
  121.       TabStop         =   0   'False
  122.       Top             =   3960
  123.       Width           =   7935
  124.    End
  125.    Begin VB.TextBox Mailtxt 
  126.       Appearance      =   0  '2D
  127.       BackColor       =   &H00FFFFFF&
  128.       BeginProperty Font 
  129.          Name            =   "Courier"
  130.          Size            =   9.6
  131.          Charset         =   0
  132.          Weight          =   400
  133.          Underline       =   0   'False
  134.          Italic          =   0   'False
  135.          Strikethrough   =   0   'False
  136.       EndProperty
  137.       Height          =   1965
  138.       Left            =   120
  139.       MultiLine       =   -1  'True
  140.       ScrollBars      =   2  'Vertikal
  141.       TabIndex        =   5
  142.       Top             =   1920
  143.       Width           =   7935
  144.    End
  145.    Begin VB.Label Process 
  146.       BackColor       =   &H00C0C0C0&
  147.       Height          =   255
  148.       Left            =   120
  149.       TabIndex        =   15
  150.       Top             =   4680
  151.       Width           =   7935
  152.    End
  153.    Begin VB.Label ggg 
  154.       Alignment       =   2  'Zentriert
  155.       AutoSize        =   -1  'True
  156.       BackColor       =   &H00000000&
  157.       Caption         =   "Server:"
  158.       ForeColor       =   &H00FFFFFF&
  159.       Height          =   195
  160.       Left            =   105
  161.       TabIndex        =   13
  162.       Top             =   360
  163.       Width           =   525
  164.    End
  165.    Begin VB.Label Label2 
  166.       BackColor       =   &H00000000&
  167.       Caption         =   "To:"
  168.       ForeColor       =   &H00FFFFFF&
  169.       Height          =   255
  170.       Left            =   240
  171.       TabIndex        =   12
  172.       Top             =   840
  173.       Width           =   375
  174.    End
  175.    Begin VB.Label Label3 
  176.       BackColor       =   &H00000000&
  177.       Caption         =   "From:"
  178.       ForeColor       =   &H00FFFFFF&
  179.       Height          =   255
  180.       Left            =   240
  181.       TabIndex        =   11
  182.       Top             =   1200
  183.       Width           =   495
  184.    End
  185.    Begin VB.Label Label4 
  186.       BackColor       =   &H00000000&
  187.       Caption         =   "Subject:"
  188.       ForeColor       =   &H00FFFFFF&
  189.       Height          =   255
  190.       Left            =   120
  191.       TabIndex        =   10
  192.       Top             =   1560
  193.       Width           =   615
  194.    End
  195. Attribute VB_Name = "Form1"
  196. Attribute VB_GlobalNameSpace = False
  197. Attribute VB_Creatable = False
  198. Attribute VB_PredeclaredId = True
  199. Attribute VB_Exposed = False
  200. Dim bTrans As Boolean
  201. Dim m_iStage As Integer
  202. Dim Sock As Integer
  203. Dim RC As Integer
  204. Dim Bytes As Integer
  205. Dim ResponseCode As Integer
  206. Dim path As Variant
  207. '*****************************************
  208. 'For the Mime File Field!
  209. '*****************************************
  210. Private Type OPENFILENAME
  211.        lStructSize As Long
  212.        hwndOwner As Long
  213.        hInstance As Long
  214.        lpstrFilter As String
  215.        lpstrCustomFilter As String
  216.        nMaxCustFilter As Long
  217.        nFilterIndex As Long
  218.        lpstrFile As String
  219.        nMaxFile As Long
  220.        lpstrFileTitle As String
  221.        nMaxFileTitle As Long
  222.        lpstrInitialDir As String
  223.        lpstrTitle As String
  224.        flags As Long
  225.        nFileOffset As Integer
  226.        nFileExtension As Integer
  227.        lpstrDefExt As String
  228.        lCustData As Long
  229.        lpfnHook As Long
  230.        lpTemplateName As String
  231. End Type
  232. Const OFN_READONLY = &H1
  233. Const OFN_OVERWRITEPROMPT = &H2
  234. Const OFN_HIDEREADONLY = &H4
  235. Const OFN_NOCHANGEDIR = &H8
  236. Const OFN_SHOWHELP = &H10
  237. Const OFN_ENABLEHOOK = &H20
  238. Const OFN_ENABLETEMPLATE = &H40
  239. Const OFN_ENABLETEMPLATEHANDLE = &H80
  240. Const OFN_NOVALIDATE = &H100
  241. Const OFN_ALLOWMULTISELECT = &H200
  242. Const OFN_EXTENSIONDIFFERENT = &H400
  243. Const OFN_PATHMUSTEXIST = &H800
  244. Const OFN_FILEMUSTEXIST = &H1000
  245. Const OFN_CREATEPROMPT = &H2000
  246. Const OFN_SHAREAWARE = &H4000
  247. Const OFN_NOREADONLYRETURN = &H8000
  248. Const OFN_NOTESTFILECREATE = &H10000
  249. Const OFN_NONETWORKBUTTON = &H20000
  250. Const OFN_NOLONGNAMES = &H40000 ' force no long names for 4.x modules
  251. Const OFN_EXPLORER = &H80000 ' new look commdlg
  252. Const OFN_NODEREFERENCELINKS = &H100000
  253. Const OFN_LONGNAMES = &H200000 ' force long names for 3.x modules
  254. Const OFN_SHAREFALLTHROUGH = 2
  255. Const OFN_SHARENOWARN = 1
  256. Const OFN_SHAREWARN = 0
  257. Private Declare Function GetSaveFileName Lib "comdlg32" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
  258. 'This is for the WaitforResponse Routine
  259. Private Declare Function timeGetTime Lib "winmm.dll" () As Long
  260. 'Dec's for the X disabling
  261. Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
  262. Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
  263. Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
  264. Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
  265. Const MF_BYPOSITION = &H400&
  266. Const MF_REMOVE = &H1000&
  267. 'For MIME processing
  268. Dim Mime As Boolean
  269. 'For Filehandling
  270. Dim Mimefilename As String
  271. Dim Mimefiles As Integer
  272. Sub DisableX(frm As Form)
  273.      Dim hMenu As Long
  274.      Dim nCount As Long
  275.      hMenu = GetSystemMenu(frm.hWnd, 0)
  276.      nCount = GetMenuItemCount(hMenu)
  277.      'Get rid of the Close menu and its separator
  278.      Call RemoveMenu(hMenu, nCount - 1, MF_REMOVE Or MF_BYPOSITION)
  279.      Call RemoveMenu(hMenu, nCount - 2, MF_REMOVE Or MF_BYPOSITION)
  280.      'Make sure the screen updates
  281.      'our change
  282.      DrawMenuBar frm.hWnd
  283. End Sub
  284. '***************************************************************
  285. 'Thanks to Luis Cantero for this Routines
  286. Sub Startrek(frm As Form)
  287. GotoVal = frm.Height / 2
  288. For Gointo = 1 To GotoVal
  289. DoEvents
  290. frm.Height = frm.Height - 100
  291. frm.Top = (Screen.Height - frm.Height) \ 2
  292. If frm.Height <= 500 Then Exit For
  293. Next Gointo
  294. horiz:
  295. frm.Height = 30
  296. GotoVal = frm.Width / 2
  297. For Gointo = 1 To GotoVal
  298. DoEvents
  299. frm.Width = frm.Width - 100
  300. frm.Left = (Screen.Width - frm.Width) \ 2
  301. If frm.Width <= 2000 Then Exit For
  302. Next Gointo
  303. End Sub
  304. Function SaveDialog(Form1 As Form, Filter As String, Title As String, InitDir As String) As String
  305. Dim ofn As OPENFILENAME
  306. Dim A As Long
  307. ofn.lStructSize = Len(ofn)
  308. ofn.hwndOwner = Form1.hWnd
  309. ofn.hInstance = App.hInstance
  310. If Right$(Filter, 1) <> "|" Then Filter = Filter + "|"
  311. For A = 1 To Len(Filter)
  312. If Mid$(Filter, A, 1) = "|" Then Mid$(Filter, A, 1) = Chr$(0)
  313. ofn.lpstrFilter = Filter
  314. ofn.lpstrFile = Space$(254)
  315. ofn.nMaxFile = 255
  316. ofn.lpstrFileTitle = Space$(254)
  317. ofn.nMaxFileTitle = 255
  318. ofn.lpstrInitialDir = InitDir
  319. ofn.lpstrTitle = Title
  320. ofn.flags = OFN_HIDEREADONLY Or OFN_CREATEPROMPT
  321. A = GetSaveFileName(ofn)
  322. If (A) Then
  323. SaveDialog = Left$(Trim$(ofn.lpstrFile), Len(Trim$(ofn.lpstrFile)) - 1)
  324. Mimefilename = Left$(Trim$(ofn.lpstrFileTitle), Len(Trim$(ofn.lpstrFileTitle)) - 1)
  325. SaveDialog = ""
  326. End If
  327. End Function
  328. '***************************************************************
  329. Private Sub Attachement_Click()
  330. Mime = True
  331. Mimefiles = Mimefiles + 1
  332. path = SaveDialog(Me, "*.*", "Attache file as", App.path)
  333. Form1.AttachementList.List(Mimefiles - 1) = path
  334. End Sub
  335. Private Sub delattach_Click()
  336. If Form1.AttachementList.List(AttachementList.ListIndex) <> "" Then
  337. path = ""
  338. Form1.AttachementList.List(AttachementList.ListIndex) = ""
  339. Mimefiles = Mimefiles - 1
  340. End If
  341. End Sub
  342. '***************************************************************
  343. 'Routine for connecting to the server
  344. '***************************************************************
  345. Private Sub SendMimeConnect_Click()
  346. ' Little Error check
  347. If Tobox.Text = "" Or InStr(Tobox.Text, "@") = 0 Then
  348. MsgBox "To: Is not correct!"
  349. Exit Sub
  350. End If
  351. Dim StartupData As WSADataType
  352. Dim SocketBuffer As sockaddr
  353. Dim IpAddr As Long
  354. 'Ini the Winsocket
  355. RC = WSAStartup(&H101, StartupData)
  356. RC = WSAStartup(&H101, StartupData)
  357. 'Open a free Socket (with this source code you can also
  358. 'open several connections! Very useful for E-Mail Applications...)
  359. Sock = socket(AF_INET, SOCK_STREAM, 0)
  360. If Sock = SOCKET_ERROR Then
  361.     Process.Caption = "Cannot Create Socket."
  362.     Exit Sub
  363. End If
  364. 'Checks if the Hostname exists
  365. If RC = SOCKET_ERROR Then Exit Sub
  366. IpAddr = GetHostByNameAlias(MailServer)
  367. If IpAddr = -1 Then
  368.     Process.Caption = "Unknown Host: " + MailServer
  369.     Exit Sub
  370. End If
  371. 'This part is responsible for the connection
  372. SocketBuffer.sin_family = AF_INET
  373. SocketBuffer.sin_port = htons(25)
  374. SocketBuffer.sin_addr = IpAddr
  375. SocketBuffer.sin_zero = String$(8, 0)
  376. RC = connect(Sock, SocketBuffer, Len(SocketBuffer))
  377. 'If an error occured close the connection and
  378. 'send an error message to the text window
  379. If RC = SOCKET_ERROR Then
  380.         Process.Caption = "Cannot Connect to " + MailServer + _
  381.                             Chr$(13) + Chr$(10) + _
  382.                             GetWSAErrorString(WSAGetLastError())
  383.         closesocket Sock
  384.         RC = WSACleanup()
  385.         Exit Sub
  386. Process.Caption = "Connected to " & MailServer.Text
  387. End If
  388. 'Select Receive Window
  389. RC = WSAAsyncSelect(Sock, DataArrival.hWnd, _
  390.                         ByVal &H202, ByVal FD_READ Or FD_CLOSE)
  391.     If RC = SOCKET_ERROR Then
  392.         Process.Caption = "Cannot Process Asynchronously."
  393.         closesocket Sock
  394.         RC = WSACleanup()
  395.         Exit Sub
  396.     End If
  397. bTrans = True
  398. m_iStage = 0
  399. DataArrival = ""
  400. ResponseCode = 220
  401. Call WaitForResponse
  402. End Sub
  403. Private Sub Exit_Click()
  404. On Error Resume Next
  405. Call Startrek(Me)
  406. closesocket Sock
  407. RC = WSACleanup()
  408. End Sub
  409. Private Sub Form_Load()
  410. Call DisableX(Me)
  411. End Sub
  412. '***************************************************************
  413. 'Routine for arraving Data
  414. '***************************************************************
  415. Private Sub DataArrival_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  416. Dim MsgBuffer As String * 2048
  417. On Error Resume Next
  418.     If Sock > 0 Then
  419.         'Receive up to 2048 chars
  420.         Bytes = recv(Sock, ByVal MsgBuffer, 2048, 0)
  421.         
  422.         If Bytes > 0 Then
  423.             
  424.         DataArrival = DataArrival + _
  425.                             MsgBuffer + _
  426.                             Chr$(13) + Chr$(10)
  427.          'Scrolls down the Textbox
  428.          DataArrival.SelStart = Len(DataArrival)
  429.          
  430.         If bTrans Then
  431.             'Checks if the Response code is correct
  432.             If ResponseCode = Left(MsgBuffer, 3) Then
  433.             MsgBuffer = vbNullString
  434.             m_iStage = m_iStage + 1
  435.             Transmit m_iStage
  436.             Else
  437.             'If the Response Code is not right reset the connection
  438.                 closesocket (Sock)
  439.                 RC = WSACleanup()
  440.                 Sock = 0
  441.                 Process.Caption = "The Server responds with an unexpected Response Code!"
  442.                 Exit Sub
  443.             End If
  444.         End If
  445.         ElseIf WSAGetLastError() <> WSAEWOULDBLOCK Then
  446.             closesocket (Sock)
  447.             RC = WSACleanup()
  448.             Sock = 0
  449.         End If
  450.     End If
  451. Refresh
  452. End Sub
  453. '***************************************************************
  454. 'Sends the E-Mail
  455. '***************************************************************
  456. Private Sub Transmit(iStage As Integer)
  457. Dim Helo As String
  458. Dim pos As Integer
  459. Select Case m_iStage
  460. Case 1:
  461. Helo = Frombox.Text
  462. pos = Len(Helo) - InStr(Helo, "@")
  463. Helo = Right$(Helo, pos)
  464. ResponseCode = 250
  465. WinsockSendData ("HELO " & Helo & vbCrLf)
  466. Call WaitForResponse
  467. Case 2:
  468. ResponseCode = 250
  469. WinsockSendData ("MAIL FROM: <" & Trim(Frombox.Text) & ">" & vbCrLf)
  470. Call WaitForResponse
  471. Case 3:
  472. ResponseCode = 250
  473. WinsockSendData ("RCPT TO: <" & Trim(Tobox.Text) & ">" & vbCrLf)
  474. Call WaitForResponse
  475. Case 4:
  476. ResponseCode = 354
  477. WinsockSendData ("DATA" & vbCrLf)
  478. Call WaitForResponse
  479. Case 5:
  480. ' Calls the routine to send the Header
  481. ResponseCode = 250
  482. Call SendMimetxt(Frombox.Text, Tobox.Text, Subjekt.Text, Mailtxt.Text, Form1.AttachementList.List(0))
  483. Call WaitForResponse
  484. 'Finish the E-Mail sending process
  485. Case 6:
  486. ResponseCode = 221
  487. WinsockSendData ("QUIT" & vbCrLf)
  488. Process.Caption = "E-Mail was sended!"
  489. m_iStage = 0
  490. bTrans = False
  491. Call WaitForResponse
  492. End Select
  493. End Sub
  494. '***************************************************************
  495. 'Routine for sending a MIME txt
  496. '***************************************************************
  497. Sub SendMimetxt(txtFrom, txtTo, txtSubjekt, txtMail, txtMimePath)
  498. Dim temp As Variant
  499. If txtMimePath <> "" Then
  500. 'Prepare the MIME Mail Header
  501. '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  502. 'If you want additional Headers like Date,Message-Id,...etc. !
  503. 'simply add them below                                      !
  504. '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  505. temp = temp & "From: " & txtFrom & vbNewLine
  506. temp = temp & "To: " & txtTo & vbNewLine
  507. temp = temp & "Subject: " & txtSubjekt & vbNewLine
  508. 'Do not change this Headers
  509. temp = temp & "Mime-Version: 1.0" & vbNewLine
  510. temp = temp & vbCrLf & "Content-Type: multipart/mixed; boundary=" + Chr(34) + "NextMimePart" + Chr(34) + vbNewLine
  511. temp = temp & "This is a multi-part message in MIME format." + vbNewLine
  512. temp = temp & "--NextMimePart" + vbNewLine
  513. 'Header plus Message
  514. temp = temp + vbCrLf + Mailtxt.Text
  515. 'Send the Mime Header and the Message
  516. WinsockSendData (temp & vbCrLf)
  517. 'Call Attachement Routine
  518. SendMimeAttachement (txtMimePath)
  519. 'Send the E-Mail without Attachement
  520. temp = temp & "From: " & txtFrom & vbNewLine
  521. temp = temp & "To: " & txtTo & vbNewLine
  522. temp = temp & "Subject: " & txtSubjekt & vbNewLine
  523. temp = temp & vbCrLf & txtMail
  524. 'Send Data and finish it!
  525. WinsockSendData (temp)
  526. WinsockSendData (vbCrLf & "." & vbCrLf)
  527. End If
  528. End Sub
  529. '**************************************************************
  530. 'NEW! Waits until time out, while waiting for response
  531. '**************************************************************
  532. Private Sub WaitForResponse()
  533. Dim Start As Long
  534. Dim Tmr As Long
  535. 'Works with an Api Declaration because it's more precious
  536. Start = timeGetTime
  537. While Bytes > 0
  538.     Tmr = timeGetTime - Start
  539.     DoEvents ' Let System keep checking for incoming response
  540.         
  541.     'Wait 50 (50000 Miliseconds) seconds for response
  542.     If Tmr > 50000 Then
  543.         Process.Caption = "SMTP service error, timed out while waiting for response"
  544.         End
  545.     End If
  546. End Sub
  547. '***************************************************************
  548. 'Routine for sending a MIME Attachement
  549. '***************************************************************
  550. Private Sub SendMimeAttachement(path As Variant)
  551. 'Dim Global
  552. Dim l As Long, i As Long, FileIn As Long
  553. Dim temp As Variant
  554. 'For Encoding BASE64
  555. Dim b As Integer
  556. Dim Base64Tab As Variant
  557. Dim bin(3) As Byte
  558. Dim s As Variant
  559. 'Base64Tab holds the encode tab
  560. Base64Tab = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/")
  561. 'Gets the next free filenumber
  562. FileIn = FreeFile
  563. 'Open Base64 Input File
  564. Open path For Binary As FileIn
  565. 'Preparing the Mime Header
  566. temp = vbCrLf + "--NextMimePart" + vbNewLine
  567. temp = temp + "Content-Type: application/octet-stream; name=" + Chr(34) + Mimefilename + Chr(34) + vbNewLine
  568. temp = temp + "Content-Transfer-Encoding: base64" + vbNewLine
  569. temp = temp + "Content-Disposition: attachment; filename=" + Chr(34) + Mimefilename + Chr(34) + vbNewLine
  570. WinsockSendData (temp & vbCrLf)
  571. l = LOF(FileIn) - (LOF(FileIn) Mod 3)
  572. For i = 1 To l Step 3
  573. 'Read three bytes
  574. Get FileIn, , bin(0)
  575. Get FileIn, , bin(1)
  576. Get FileIn, , bin(2)
  577. 'Always wait until there're more then 64 characters
  578. If Len(s) > 64 Then
  579.     Process.Caption = "Send Attachement..." & i & " Bytes from " & l
  580.     DoEvents
  581.     s = s + vbCrLf
  582.     WinsockSendData (s)
  583.     s = ""
  584. End If
  585. 'Calc Base64-encoded char
  586.     b = (bin(0) \ 4) And &H3F 'right shift 2 bits (&H3F=111111b)
  587.     'the character s holds the encoded chars
  588.     s = s + Base64Tab(b)
  589.     b = ((bin(0) And &H3) * 16) Or ((bin(1) \ 16) And &HF)
  590.     s = s + Base64Tab(b)
  591.     b = ((bin(1) And &HF) * 4) Or ((bin(2) \ 64) And &H3)
  592.     s = s + Base64Tab(b)
  593.     b = bin(2) And &H3F
  594.     s = s + Base64Tab(b)
  595.  Next i
  596. 'Now, you need to check if there is something left
  597. If Not (LOF(FileIn) Mod 3 = 0) Then
  598. 'Reads the number of bytes left
  599. For i = 1 To (LOF(FileIn) Mod 3)
  600.     Get FileIn, , bin(i - 1)
  601. Next i
  602. 'If there are only 2 chars left
  603. If (LOF(FileIn) Mod 3) = 2 Then
  604.     b = (bin(0) \ 4) And &H3F 'right shift 2 bits (&H3F=111111b)
  605.     s = s + Base64Tab(b)
  606.     b = ((bin(0) And &H3) * 16) Or ((bin(1) \ 16) And &HF)
  607.     s = s + Base64Tab(b)
  608.     b = ((bin(1) And &HF) * 4) Or ((bin(2) \ 64) And &H3)
  609.     s = s + Base64Tab(b)
  610.     s = s + "="
  611. 'If there is only one char left
  612.     b = (bin(0) \ 4) And &H3F 'right shift 2 bits (&H3F=111111b)
  613.     s = s + Base64Tab(b)
  614.     b = ((bin(1) And &H3) * 16) Or ((bin(1) \ 16) And &HF)
  615.     s = s + Base64Tab(b)
  616.     s = s + "=="
  617. End If
  618. End If
  619. 'Send the characters left
  620. If s <> "" Then
  621.     s = s & vbCrLf
  622.     WinsockSendData (s)
  623. End If
  624. 'Send the last part of the MIME Body
  625. WinsockSendData (vbCrLf & "--NextMimePart--" & vbCrLf)
  626. WinsockSendData (vbCrLf & "." & vbCrLf)
  627. Close FileIn
  628. End Sub
  629. Private Sub WinsockSendData(DatatoSend As String)
  630. Dim RC As Integer
  631. Dim MsgBuffer As String * 2048
  632. MsgBuffer = DatatoSend
  633. 'You can open more than one connection!
  634. RC = send(Sock, ByVal MsgBuffer, Len(DatatoSend), 0)
  635. 'If an error occurs send an error message and
  636. 'reset the winsock
  637. If RC = SOCKET_ERROR Then
  638.     Process.Caption = "Cannot Send Request." + _
  639.                             Chr$(13) + Chr$(10) + _
  640.                             Str$(WSAGetLastError()) + _
  641.                             GetWSAErrorString(WSAGetLastError())
  642.     closesocket Sock
  643.     RC = WSACleanup()
  644.     Exit Sub
  645. End If
  646. End Sub
  647.