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