*/
Written some cool source code? Upload it to Programmer's Heaven.
*/

View \Mail.bas

WinDOS Shell 0.1A

Submitted By: Shehbaz
Rating: starstarstarstarstar (Rate It)


Attribute VB_Name = "MailModule"
OPTION Explicit
Public CONST SW_SHOWNORMAL AS LONG = 1
Public DECLARE FUNCTION ShellExecute Lib "shell32.dll" ALIAS "ShellExecuteA" (BYVAL hwnd AS LONG, BYVAL lpOperation AS STRING, BYVAL lpFile AS STRING, BYVAL lpParameters AS STRING, BYVAL lpDirectory AS STRING, BYVAL nShowCmd AS LONG) AS LONG

' Base64Encode(strOriginal)
' Base64Encode("the") would return "dGjl"
' You can only pass three letters as the arguement
Public FUNCTION Base64Encode(strOriginal AS STRING)
    DIM intCount AS INTEGER
    DIM strBinary AS STRING
    DIM intDecimal AS INTEGER
    DIM strTemp AS STRING

    intDecimal = ASC(LEFT$(strOriginal, 1))
   
    FOR intCount = 7 TO 0 STEP -1
        IF (2 ^ intCount) <= intDecimal THEN
            strBinary = strBinary & "1"
            intDecimal = intDecimal - (2 ^ intCount)
        ELSE
            strBinary = strBinary & "0"
        END IF
    NEXT
   
    IF LEN(strOriginal) < 3 THEN GOTO unfpassone
   
    intDecimal = ASC(MID$(strOriginal, 2, 1))
   
    FOR intCount = 7 TO 0 STEP -1
        IF (2 ^ intCount) <= intDecimal THEN
            strBinary = strBinary & "1"
            intDecimal = intDecimal - (2 ^ intCount)
        ELSE
            strBinary = strBinary & "0"
        END IF
    NEXT
   
    IF LEN(strOriginal) < 3 THEN GOTO unfpassone
   
    intDecimal = ASC(RIGHT$(strOriginal, 1))
   
    FOR intCount = 7 TO 0 STEP -1
        IF (2 ^ intCount) <= intDecimal THEN
            strBinary = strBinary & "1"
            intDecimal = intDecimal - (2 ^ intCount)
        ELSE
            strBinary = strBinary & "0"
        END IF
    NEXT
   
unfpassone:
    FOR intCount = 1 TO 19 STEP 6
        SELECT CASE VAL(MID$(strBinary, intCount, 6))
            CASE 0
                strTemp = strTemp & "A"
            CASE 1
                strTemp = strTemp & "B"
            CASE 10
                strTemp = strTemp & "C"
            CASE 11
                strTemp = strTemp & "D"
            CASE 100
                strTemp = strTemp & "E"
            CASE 101
                strTemp = strTemp & "F"
            CASE 110
                strTemp = strTemp & "G"
            CASE 111
                strTemp = strTemp & "H"
            CASE 1000
                strTemp = strTemp & "I"
            CASE 1001
                strTemp = strTemp & "J"
            CASE 1010
                strTemp = strTemp & "K"
            CASE 1011
                strTemp = strTemp & "L"
            CASE 1100
                strTemp = strTemp & "M"
            CASE 1101
                strTemp = strTemp & "N"
            CASE 1110
                strTemp = strTemp & "O"
            CASE 1111
                strTemp = strTemp & "P"
            CASE 10000
                strTemp = strTemp & "Q"
            CASE 10001
                strTemp = strTemp & "R"
            CASE 10010
                strTemp = strTemp & "S"
            CASE 10011
                strTemp = strTemp & "T"
            CASE 10100
                strTemp = strTemp & "U"
            CASE 10101
                strTemp = strTemp & "V"
            CASE 10110
                strTemp = strTemp & "W"
            CASE 10111
                strTemp = strTemp & "X"
            CASE 11000
                strTemp = strTemp & "Y"
            CASE 11001
                strTemp = strTemp & "Z"
            CASE 11010
                strTemp = strTemp & "a"
            CASE 11011
                strTemp = strTemp & "b"
            CASE 11100
                strTemp = strTemp & "c"
            CASE 11101
                strTemp = strTemp & "d"
            CASE 11110
                strTemp = strTemp & "e"
            CASE 11111
                strTemp = strTemp & "f"
            CASE 100000
                strTemp = strTemp & "g"
            CASE 100001
                strTemp = strTemp & "h"
            CASE 100010
                strTemp = strTemp & "i"
            CASE 100011
                strTemp = strTemp & "j"
            CASE 100100
                strTemp = strTemp & "k"
            CASE 100101
                strTemp = strTemp & "l"
            CASE 100110
                strTemp = strTemp & "m"
            CASE 100111
                strTemp = strTemp & "n"
            CASE 101000
                strTemp = strTemp & "o"
            CASE 101001
                strTemp = strTemp & "p"
            CASE 101010
                strTemp = strTemp & "q"
            CASE 101011
                strTemp = strTemp & "r"
            CASE 101100
                strTemp = strTemp & "s"
            CASE 101101
                strTemp = strTemp & "t"
            CASE 101110
                strTemp = strTemp & "u"
            CASE 101111
                strTemp = strTemp & "v"
            CASE 110000
                strTemp = strTemp & "w"
            CASE 110001
                strTemp = strTemp & "x"
            CASE 110010
                strTemp = strTemp & "y"
            CASE 110011
                strTemp = strTemp & "z"
            CASE 110100
                strTemp = strTemp & "0"
            CASE 110101
                strTemp = strTemp & "1"
            CASE 110110
                strTemp = strTemp & "2"
            CASE 110111
                strTemp = strTemp & "3"
            CASE 111000
                strTemp = strTemp & "4"
            CASE 111001
                strTemp = strTemp & "5"
            CASE 111010
                strTemp = strTemp & "6"
            CASE 111011
                strTemp = strTemp & "7"
            CASE 111100
                strTemp = strTemp & "8"
            CASE 111101
                strTemp = strTemp & "9"
            CASE 111110
                strTemp = strTemp & "+"
            CASE 111111
                strTemp = strTemp & "/"
        END SELECT
    NEXT
   
    Base64Encode = strTemp
   
END FUNCTION

' Base64EncodeFile(strFile,rtfTemp,txtOutput)
' Base64EncodeFile "c:\windows\autoexec.bat",rtfBox,txtBox
' The second parameter must be a rtf box or a control that supports the
' LoadFile command

Public SUB Base64EncodeFile(strFile AS STRING, rtfTemp AS RichTextBox, txtOutput AS TextBox)
   
    DIM intCount AS INTEGER
    DIM strTemp AS STRING
    DIM lngMax AS LONG

    lngMax = 0
    txtOutput.Text = ""
    rtfTemp.LoadFile strFile
   
    FOR intCount = 1 TO LEN(rtfTemp.Text) STEP 3
   
        strTemp = Mid(rtfTemp.Text, intCount, 3)
        txtOutput.Text = txtOutput.Text & Base64Encode(strTemp)
        lngMax = lngMax + 4
       
        IF lngMax = 72 THEN
            lngMax = 0
            txtOutput.Text = txtOutput.Text & vbCrLf
        END IF
       
        DoEvents
    NEXT intCount
   
END SUB

' ConnectToServer(strServer, wsk, strSrvPort)
' ConnectToServer "pop.microsoft.com", Winsock1, 25
' Normally leave out the last arguement and let the Winsock control use
' the default port.

Public SUB ConnectToServer(strServer AS STRING, wsk AS Winsock, Optional strSrvPort AS STRING)
   
    wsk.RemoteHost = strServer
   
    IF strSrvPort = "" THEN
        wsk.RemotePort = 25
    ELSE
        wsk.RemotePort = VAL(strSrvPort)
    END IF
   
    wsk.Connect

END SUB

' ExtractArgument(ArgNum, srchstr, Delim)
' ExtractArgument(3, "No 1, No 2, No 3", ",") Would return No 3
' I did not have time to sort out the variable names in this function,
' so if you can be bothered to, please send it to me at [[Email Removed]]

Private FUNCTION ExtractArgument(ArgNum AS INTEGER, srchstr AS STRING, Delim AS STRING) AS STRING

    ON ERROR GOTO Err_ExtractArgument
   
    DIM ArgCount AS INTEGER
    DIM LastPos AS INTEGER
    DIM POS AS INTEGER
    DIM Arg AS STRING
   
    Arg = ""
    LastPos = 1
    IF ArgNum = 1 THEN Arg = srchstr
    DO WHILE INSTR(srchstr, Delim) > 0
        POS = INSTR(LastPos, srchstr, Delim)
        IF POS = 0 THEN
            IF ArgCount = ArgNum - 1 THEN Arg = Mid(srchstr, LastPos)
            EXIT DO
        ELSE
            ArgCount = ArgCount + 1
            IF ArgCount = ArgNum THEN
                Arg = Mid(srchstr, LastPos, POS - LastPos)
                EXIT DO
            END IF
        END IF
        LastPos = POS + 1
    LOOP
    ExtractArgument = Arg
   
    EXIT FUNCTION
   
Err_ExtractArgument:
    MsgBox "Error " & ERR & ": " & ERROR
    RESUME NEXT
END FUNCTION

' SendMail(strFrom, strTo, strSubject, strBody, wsk, strAttachName, txtEncodedFile)
' SendMail "[[Email Removed]]", "[[Email Removed]]", "Test Message", "Body", Winsock1, "myfile.ext", txtEncodedFile
' If you omit the last two arguements then no file is attached
' Before attaching a file, you must first encode it using the Base64EncodeFile function

Public SUB SendMail(strFrom AS STRING, strTo AS STRING, strSubject AS STRING, strBody AS TextBox, wsk AS Winsock, Optional strAttachName AS STRING, Optional txtEncodedFile AS Control)
   
    DIM intCount AS INTEGER
   
    WAIT 0.5
   
    wsk.SendData "EHLO " & wsk.LocalIP & vbCrLf
    wsk.SendData "MAIL FROM:" & strFrom & vbCrLf
   
    WAIT 0.5
   
    wsk.SendData "RCPT TO:" & strTo & vbCrLf
    wsk.SendData "DATA" & vbCrLf
   
    WAIT 0.5
   
    wsk.SendData "MIME-Version: 1.0" & vbCrLf
    wsk.SendData "From: " & ExtractArgument(1, strFrom, "@") & " <" & strFrom & ">" & vbCrLf
    wsk.SendData "To: <" & strTo & ">" & vbCrLf
    wsk.SendData "Subject: " & strSubject & vbCrLf
    wsk.SendData "Content-Type: multipart/mixed;" & vbCrLf
    wsk.SendData "              boundary=Unique-Boundary" & vbCrLf & vbCrLf
    wsk.SendData " [ Random garbage here ]" & vbCrLf & vbCrLf
    wsk.SendData vbCrLf & "--Unique-Boundary" & vbCrLf
    wsk.SendData "Content-type: text/plain; charset=US-ASCII" & vbCrLf & vbCrLf
    wsk.SendData strBody.Text & vbCrLf & vbCrLf
   
    IF LTrim(RTrim(strAttachName)) <> "" THEN
   
        FOR intCount = LEN(strAttachName) TO 1 STEP -1
       
            IF Mid(strAttachName, intCount, 1) = "\" THEN
                strAttachName = Mid(strAttachName, intCount + 1)
                GOTO lala
            END IF
           
        NEXT intCount
lala:
        wsk.SendData "--Unique-Boundary" & vbCrLf
        wsk.SendData "Content-Type: multipart/parallel; boundary=Unique-Boundary-2" & vbCrLf & vbCrLf
        wsk.SendData "--Unique-Boundary-2" & vbCrLf
        wsk.SendData "Content-Type: application/octet-stream;" & vbCrLf
        wsk.SendData " name=" & strAttachName & vbCrLf
        wsk.SendData "Content-Transfer-Encoding: base64" & vbCrLf
        wsk.SendData "Content-Disposition: inline;" & vbCrLf
        wsk.SendData " filename=" & strAttachName & vbCrLf & vbCrLf
        wsk.SendData txtEncodedFile.Text & "==" & vbCrLf
        wsk.SendData "--Unique-Boundary-2----Unique-Boundary--"
       
    END IF
   
    wsk.SendData vbCrLf & "." & vbCrLf
   
    WAIT 0.5
   
    wsk.SendData "QUIT" & vbCrLf
   
    WAIT 0.5
   
    wsk.CLOSE
   
END SUB

' Wait(WaitTime)
' Wait 0.5

Public SUB WAIT(WaitTime)

    DIM StartTime AS DOUBLE
   
    StartTime = TIMER
   
    DO WHILE TIMER < StartTime + WaitTime
        IF TIMER > 86395 OR TIMER = 0 THEN EXIT DO
        DoEvents
    LOOP
   
END SUB

corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.