How do I send an attachment using the Winsock control 6.0

I'm using the Winsock control to send email.  I've got the code to send all the different email parts using the SendData method but I need to know what to pass for an attachment.  I also need to know does the attachment need to be encoded first with MIME or something.  If you know these answers, and how to do it, your help would be GREATLY appreciated.
carlosdgonzalezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcriderCommented:
Here is an example of a subroutine that sends an email with an attachment.  The last argument of the subroutine is a pathname to a file that has been UUENCODED.  This UUENCODED file is the attachment.  The subroutine assumes that the winsock control has already connected to the mail server...

    Sub SendMessage(FromAddr As String, ToAddr As String, Subject As String, Msg As String, Attachment As String)
        Dim fNum As Integer
        Dim AttachData As String
       
        With Winsock1
            .SendData "HELO " + CStr(.LocalIP) + vbCrLf
            DoEvents
            .SendData "MAIL FROM: <" + FromAddr + ">" + vbCrLf
            DoEvents
            .SendData "RCPT TO: <" + ToAddr + ">" + vbCrLf
            DoEvents
            .SendData "DATA" + vbCrLf
            DoEvents
            .SendData "SUBJECT: " + Subject + vbCrLf
            DoEvents
            .SendData "FROM: " + FromAddr + vbCrLf
            DoEvents
            .SendData "DATE: " + CStr(Now) + vbCrLf + vbCrLf
            DoEvents
            .SendData Msg + vbCrLf + vbCrLf + vbCrLf
            DoEvents
            If Not Attachment = "" Then
                DoEvents
                fNum = FreeFile
                Open Attachment For Binary Access Read As fNum
                AttachData = Space(LOF(fNum))
                Get #fNum, , AttachData
                Close #fNum
                .SendData AttachData
                DoEvents
            End If
            .SendData vbCrLf + vbCrLf + "." + vbCrLf
            DoEvents
            .SendData "QUIT" + vbCrLf
            DoEvents
        End With
    End Sub



Now for the attachment.  To UUENCODE a FILE, Add the following code to a MODULE:

    Function uuDecode(sBuffer As String) As String
        Dim iLoop As Long
        Dim sOutBuffer As String
        Dim iVal As Integer
       
        'FIND ANY HIGH SPACES AND REPLACE THEM
        iVal = 1
        Do
            iVal = InStr(iVal, sBuffer, Chr$(96))
            If iVal = 0 Then Exit Do
            Mid$(sBuffer, iVal, 1) = " "
        Loop
        'DO THE DECODE
        For iLoop = 1 To Len(sBuffer) Step 4
            sOutBuffer = sOutBuffer + _
                Chr$((Asc(Mid$(sBuffer, iLoop, 1)) - 32) * 4 + _
                (Asc(Mid$(sBuffer, iLoop + 1, 1)) - 32) \ 16)
            sOutBuffer = sOutBuffer + _
                Chr$((Asc(Mid$(sBuffer, iLoop + 1, 1)) Mod 16) * 16 + _
                (Asc(Mid$(sBuffer, iLoop + 2, 1)) - 32) \ 4)
            sOutBuffer = sOutBuffer + _
                Chr$((Asc(Mid$(sBuffer, iLoop + 2, 1)) Mod 4) * 64 + _
                Asc(Mid$(sBuffer, iLoop + 3, 1)) - 32)
        Next iLoop
        uuDecode = sOutBuffer
    End Function
    Function uuEncode(sBuffer As String) As String
        Dim iLoop As Long
        Dim sOutBuffer As String
        Dim sOutChar As String
       
        'PAD BUFFER TO MULTIPLE OF 3
        If Len(sBuffer) Mod 3 <> 0 Then sBuffer = sBuffer + _
            Space$(3 - Len(sBuffer) Mod 3)
       
        'ENCODE BUFFER REPLACING ENCODED SPACES WITH HIGH SPACES
        For iLoop = 1 To Len(sBuffer) Step 3
            '1ST UUCHAR
            sOutChar = Chr$(Asc(Mid$(sBuffer, iLoop, 1)) \ 4 + 32)
            If sOutChar = " " Then
                sOutBuffer = sOutBuffer + Chr$(96)
            Else
                sOutBuffer = sOutBuffer + sOutChar
            End If
            '2ND UUCHAR
            sOutChar = Chr$((Asc(Mid$(sBuffer, iLoop, 1)) Mod 4) * 16 + _
                Asc(Mid$(sBuffer, iLoop + 1, 1)) \ 16 + 32)
            If sOutChar = " " Then
                sOutBuffer = sOutBuffer + Chr$(96)
            Else
                sOutBuffer = sOutBuffer + sOutChar
            End If
            '3RD UUCHAR
            sOutChar = Chr$((Asc(Mid$(sBuffer, iLoop + 1, 1)) Mod 16) * 4 + _
                Asc(Mid$(sBuffer, iLoop + 2, 1)) \ 64 + 32)
            If sOutChar = " " Then
                sOutBuffer = sOutBuffer + Chr$(96)
            Else
                sOutBuffer = sOutBuffer + sOutChar
            End If
            '4TH UUCHAR
            sOutChar = Chr$(Asc(Mid$(sBuffer, iLoop + 2, 1)) Mod 64 + 32)
            If sOutChar = " " Then
                sOutBuffer = sOutBuffer + Chr$(96)
            Else
                sOutBuffer = sOutBuffer + sOutChar
            End If
        Next iLoop
        uuEncode = sOutBuffer
    End Function
    Function uuDecodeFile(sInFile As String, sOutFile As String) As Boolean
        Dim iInput As Long
        Dim iOutput As Long
        Dim iFoundHeader As Boolean
        Dim sInput As String
        Dim sOutput As String
        Dim lChar As Byte
       
        On Error Resume Next
        uuDecodeFile = False
        'DOES THE INPUT FILE EXIST?
        If Dir$(sInFile) = "" Then Exit Function
        iInput = FreeFile
        Open sInFile For Binary Access Read As #iInput
        If Err Then Exit Function
       
        'ERASE THE OUTPUT FILE IF IT EXISTS
        Kill sOutFile
        Err = 0
        iOutput = FreeFile
        Open sOutFile For Binary As #iOutput
        If Err Then Exit Function
       
        'FIND THE UUBEGIN HEADER
        iFoundHeader = False
        Do Until EOF(iInput)
            sInput = ""
            Do
                If EOF(iInput) Then Exit Do
                Get #iInput, , lChar
                Select Case lChar
                    Case 13
                    Case 10
                        Exit Do
                    Case Else
                        sInput = sInput + Chr$(lChar)
                End Select
            Loop
            If Left$(UCase$(Trim$(sInput)), 5) = "BEGIN" Then
                iFoundHeader = True
                Exit Do
            End If
        Loop
       
        If iFoundHeader = True Then
            'DECODE THE FILE
            Do Until EOF(iInput)
                sInput = ""
                Do
                    If EOF(iInput) Then Exit Do
                    Get #iInput, , lChar
                    Select Case lChar
                        Case 13
                        Case 10
                            Exit Do
                        Case Else
                            sInput = sInput + Chr$(lChar)
                    End Select
                Loop
                If UCase$(Trim$(sInput)) = "END" Then
                   Exit Do
                End If
                If Trim$(sInput) <> "" Then
                   sOutput = Left$(uuDecode(Mid$(sInput, 2, Len(sInput) - 1)), Asc(Left$(sInput, 1)) - 32)
                   Put #iOutput, , sOutput
                End If
            Loop
            uuDecodeFile = True
        End If
       
        'WE'RE DONE!
        Close iInput
        Close iOutput
    End Function
    Function uuEncodeFile(sInFile As String, sOutFile As String) As Boolean
        Dim iLoop As Long
        Dim iInput As Long
        Dim iOutput As Long
        Dim iFullLines As Long
        Dim sInput As String
        Dim sOutput As String
        Dim sFileName As String
       
        On Error Resume Next
        uuEncodeFile = False
       
        'DOES THE INPUT FILE EXIST?
        If (Dir$(sInFile) = "") Then Exit Function
        Kill sOutFile
        Err = 0
        iInput = FreeFile
        Open sInFile For Binary As #iInput
        If Err Then Exit Function
        'OPEN THE OUTPUT FILE
        iOutput = FreeFile
        Open sOutFile For Output As #iOutput
        If Err Then
            Close iInput
            Exit Function
        End If
       
        'PUT TOGETHER THE UUHEADER
        sFileName = sInFile
        For iLoop = Len(sInFile) - 1 To 1 Step -1
            If Mid$(sInFile, iLoop, 1) = "\" Then
                sFileName = Mid$(sInFile, iLoop + 1)
                Exit For
            End If
        Next iLoop
        Print #iOutput, "begin 644 " + sFileName + Chr$(10);
       
        'DETERMINE NUMBER OF LINES... EACH 45 BYTES GET EXPANDED TO 60 BYTES
        'WRITE ALL LINES UP TO LAST FULL BLOCK
        iFullLines = LOF(iInput) \ 45
        sInput = Space$(45)
        For iLoop = 1 To iFullLines
            Get #iInput, , sInput
            Print #iOutput, "M" + uuEncode(sInput) + Chr$(10);
        Next iLoop
       
        'WRITE THE LAST BLOCK
        sInput = Space$(LOF(iInput) - Seek(iInput) + 1)
        Get iInput, , sInput
        Print #iOutput, Chr$(Len(sInput) + 32) + uuEncode(sInput) + Chr$(10);
        Print #iOutput, Chr$(96) + Chr$(10) + "end" + Chr$(10);
       
        'WE'RE DONE!
        Close #iInput
        Close #iOutput
        uuEncodeFile = True
    End Function



Then you can encode a file this way:

   uuEncodeFile "C:\Windows\Desktop\TestFile.jpg", "C:\Windows\Desktop\TestFile.uue"

This will encode a jpg file called TestFile.jpg into a file called TestFile.uue


Hope this helps!


Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcriderCommented:
By the way, I also included the UUDECODE routines in the above code...

Cheers!
0
mcriderCommented:
Thanks for the points! Glad I could help!


Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.