与简单电子邮件发送程序相比,本示例程序多了框架控件及其中的内容。框架中的列表框用来显示附件的路径,另外两个按钮的作用相信你一看就知道。真正发送附件的代码在Send message按钮的Click事件中。在该事件中多了一小段代码:For i = 0 To lstAttachments.ListCount - 1
    lstAttachments.ListIndex = i
    m_strEncodedFiles = m_strEncodedFiles & _
    UUEncodeFile(lstAttachments.Text) & vbCrLf
Next i 
上面的代码将附件的路径作为参数传递给UUEncodeFile函数。该函数的作用是按照我们前面所讲的算法对字符进行编码。编码后的数据被保存在一个模块级变量m_strEncodedFile中。然后该变量的内容被添加到邮件正文中:'Add atacchments 
strMessage = txtMessage & vbCrLf & vbCrLf & m_strEncodedFiles剩下的事情就再清楚不过了。编码后的数据作为邮件的一部分发送出却,你不需编写特别的代码处理SMTP服务器。下面的函数UUEncodeFile的代码:Public Function UUEncodeFile(strFilePath As String) As StringDim intFile As Integer     'file handler
Dim intTempFile As Integer 'temp file
Dim lFileSize As Long      'size of the file
Dim strFileName As String  'name of the file
Dim strFileData As String  'file data chunk
Dim lEncodedLines As Long  'number of encoded lines
Dim strTempLine As String  'temporary string
Dim i As Long              'loop counter
Dim j As Integer           'loop counterDim strResult As String
'
'Get file name
strFileName = Mid$(strFilePath, InStrRev(strFilePath, "\") + 1)
'
'Insert first er: "begin 664 ..."
strResult = "begin 664 " + strFileName + vbLf
'
'Get file size
lFileSize = FileLen(strFilePath)
lEncodedLines = lFileSize \ 45 + 1
'
'Prepare buffer to retrieve data from
'the file by 45 symbols chunks
strFileData = Space(45)
'
intFile = FreeFile
'
Open strFilePath For Binary As intFile
For i = 1 To lEncodedLines
    'Read file data by 45-bytes cnunks
    '
    If i = lEncodedLines Then
        'Last line of encoded data often is not
        'equal to 45, therefore we need to change
        'size of the buffer
        strFileData = Space(lFileSize Mod 45)
    End If
    'Retrieve data chunk from file to the buffer
Get intFile, , strFileData
    'Add first symbol to encoded string that informs
    'about quantity of symbols in encoded string.
    'More often "M" symbol is used.
strTempLine = Chr(Len(strFileData) + 32)
    '
    If i = lEncodedLines And (Len(strFileData) Mod 3) Then
        'If the last line is processed and length of
        'source data is not a number divisible by 3, 
        'add one or two blankspace symbols
        strFileData = strFileData + Space(3 -             (Len(strFileData) Mod 3))
    End If    For j = 1 To Len(strFileData) Step 3
        'Breake each 3 (8-bits) bytes to 4 (6-bits) bytes
        '
        '1 byte
        strTempLine = strTempLine +             Chr(Asc(Mid(strFileData, j, 1)) \ 4 + 32)
        '2 byte
        strTempLine = strTempLine + _
            Chr((Asc(Mid(strFileData, j, 1)) Mod 4) * 16             + Asc(Mid(strFileData, j + 1, 1)) \ 16 + 32)
        '3 byte
        strTempLine = strTempLine + _
            Chr((Asc(Mid(strFileData, j + 1, 1)) Mod 16) * 4             + Asc(Mid(strFileData, j + 2, 1)) \ 64 + 32)
        '4 byte
        strTempLine = strTempLine + _
            Chr(Asc(Mid(strFileData, j + 2, 1)) Mod 64 + 32)
    Next j
    'add encoded line to result buffer
    strResult = strResult + strTempLine + vbLf
    'reset line buffer
    strTempLine = ""
Next i
Close intFile
'add the end er
strResult = strResult & "'" & vbLf + "end" + vbLf
'asign return value
UUEncodeFile = strResultEnd Function我不敢说上面的代码的运行速度是最快的,但却是我试验多次达到的最快速度。VB处理字符并不是它的特长,所以如果速度对你来讲至关重要的话,请尝试用C++或Delphi开发的库或组件。