我以前也遇到此现象,不过,在读eml文件时不要使用string。用二进制方式打开文件,然后一个字节一个字节的读出存入Byte类型的数组。还有,附件在发送时是被编码的应进行相应的解码才能得出附件内容。我改过人家的一个VB的例程,是将一文件附加在邮件中的编码过程,可以参考一下。在SMTP协议中有祥细说明。如下: Public Function UUEncodeFile(strFilePath As String) As String Dim 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 strTemp As String 'add by xjh Dim i As Long 'loop counter Dim j As Integer 'loop counter
Dim strResult As String
Dim strXJH(1 To 45) As Byte 'xjh Dim SpaceN As Integer Dim count As Integer Dim intFile1 As Integer 'xjh
' '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
' intFile1 = FreeFile 'add ' Open App.Path & "\temp.zip" For Binary Access Write As intFile1 'add
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) For count = 1 To Len(strFileData) Get intFile, , strXJH(count) Next GoTo Tab1 End If 'Retrieve data chunk from file to the buffer For count = 1 To 45 Get intFile, , strXJH(count) Next
'add xjh ' Put intFile1, , strFileData
'Add first symbol to encoded string that informs 'about quantity of symbols in encoded string. 'More often "M" symbol is used. Tab1: 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)) Do While count = Len(strFileData) strXJH(count) = Space(1) count = count + 1 Loop
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(strXJH(j) \ 4 + 32) '2 byte strTempLine = strTempLine + Chr((strXJH(j) Mod 4) * 16 _ + strXJH(j + 1) \ 16 + 32) '3 byte strTempLine = strTempLine + Chr((strXJH(j + 1) Mod 16) * 4 _ + strXJH(j + 2) \ 64 + 32) '4 byte strTempLine = strTempLine + Chr(strXJH(j + 2) Mod 64 + 32) Next j 'replace " " with "`" strTempLine = Replace(strTempLine, " ", "`") 'add encoded line to result buffer strResult = strResult + strTempLine + vbLf 'reset line buffer strTempLine = "" Next i Close intFile ' Close intFile1 'add 'add the end er strResult = strResult & "`" & vbLf + "end" + vbLf 'asign return value UUEncodeFile = strResult
Public Function UUEncodeFile(strFilePath As String) As String Dim 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 strTemp As String 'add by xjh
Dim i As Long 'loop counter
Dim j As Integer 'loop counter
Dim strResult As String
Dim strXJH(1 To 45) As Byte 'xjh
Dim SpaceN As Integer
Dim count As Integer
Dim intFile1 As Integer 'xjh
'
'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
' intFile1 = FreeFile 'add
' Open App.Path & "\temp.zip" For Binary Access Write As intFile1 'add
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)
For count = 1 To Len(strFileData)
Get intFile, , strXJH(count)
Next
GoTo Tab1
End If
'Retrieve data chunk from file to the buffer
For count = 1 To 45
Get intFile, , strXJH(count)
Next
'add xjh
' Put intFile1, , strFileData
'Add first symbol to encoded string that informs
'about quantity of symbols in encoded string.
'More often "M" symbol is used.
Tab1: 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))
Do While count = Len(strFileData)
strXJH(count) = Space(1)
count = count + 1
Loop
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(strXJH(j) \ 4 + 32)
'2 byte
strTempLine = strTempLine + Chr((strXJH(j) Mod 4) * 16 _
+ strXJH(j + 1) \ 16 + 32)
'3 byte
strTempLine = strTempLine + Chr((strXJH(j + 1) Mod 16) * 4 _
+ strXJH(j + 2) \ 64 + 32)
'4 byte
strTempLine = strTempLine + Chr(strXJH(j + 2) Mod 64 + 32)
Next j
'replace " " with "`"
strTempLine = Replace(strTempLine, " ", "`")
'add encoded line to result buffer
strResult = strResult + strTempLine + vbLf
'reset line buffer
strTempLine = ""
Next i
Close intFile
' Close intFile1 'add 'add the end er
strResult = strResult & "`" & vbLf + "end" + vbLf
'asign return value
UUEncodeFile = strResult
End Function