发送电子邮件附件 第二个标签页中有一个ListView控件,用来显示文件列表,以及一个按钮,用来保存附件。如下图所示: 在本示例程序中,我们对CMessage类的CreateFromText方法作了更新: Dim strFileName As String Dim strAttachment As String Dim lngFileSize As Long Dim lngFirstBegin As Long Dim lngFullStrings As Long Const ENCODED_LINE_LENGTH = 61 ' intPosA = 1 Do 'Looking for the start marker - "begin " intPosA = InStr(intPosA, strMessage, vbCrLf & "begin ") + 2 'Exit from the loop if it hasn't found If intPosA = 2 Then Exit Do 'remember the position of the first marker If lngFirstBegin = 0 Then lngFirstBegin = intPosA - 2 'looking for the end of string the marker belongs to intPosB = InStr(intPosA + 1, strMessage, vbCrLf) If intPosB > 0 Then 'check whether it is real marker or just 'an ordinal word "begin" If Mid$(strMessage, intPosA, _ intPosB - intPosA) Like "begin ### *" Then 'exctract the name of the file strFileName = Mid$(strMessage, intPosA + _ 10, intPosB - intPosA - 10) ' 'further - the code for figuring out the size of the file 'extract encoded data of the file strAttachment = Mid$(strMessage, intPosB + 2, _ InStr(intPosB + 2, strMessage, _ vbCrLf & "'" & vbCrLf & "end") _ - (intPosB + 2)) 'remove additional symbols vbCrlf strAttachment = Replace(strAttachment, vbCrLf, "") '"temporary" size of the file lngFileSize = CLng(Len(strAttachment)) 'get the number of full strings lngFullStrings = lngFileSize \ ENCODED_LINE_LENGTH If lngFileSize Mod ENCODED_LINE_LENGTH > 0 Then 'The size of the file is equal to 'the sum of the two meanings 'The first one multiplies to the number of _ 'decoded characters (45) 'The second one is the number of the bytes _ 'in the last string. It equals 'to the difference between the ASCII meaning _ 'of the fist character and 32 lngFileSize = lngFullStrings * 45 + _ (Asc(Mid$(strAttachment, _ lngFullStrings * _ ENCODED_LINE_LENGTH + 1, 1)) - 32) Else 'This code runs in very rare cases when _ 'the size of the file is multiple 'to 45 and we don't need to process _ 'non-standard last string of the encoded data lngFileSize = (lngFileSize - _ lngFileSize / ENCODED_LINE_LENGTH) * 0.75 End If 'add new item into the mvarAttachments collection mvarAttachments.Add lngFileSize, strFileName End If End If Loop 'separate the text of the message If mvarAttachments.Count > 0 Then m_strMessageBody = Left$(lngFirstBegin - 1) End If 上面的代码定义了文件名及其大小。这些信息保存在CAttachment对象中,当添中新的项目到mvarAttchments集合时,该对象即被创建。该集合是CMessage类的一个新的属性-Attachments。 下面是ListView控件的ItemClick事件中的代码: Dim oAttachment As CAttachment Dim lvItem As ListItem lvAttachments.ListItems.Clear For Each oAttachment In m_colMessages(Item.Key).Attachments Set lvItem = lvAttachments.ListItems.Add _ (, oAttachment.FileName, oAttachment.FileName) lvItem.SubItems(1) = oAttachment.Size Next txtBody = m_colMessages(Item.Key).MessageBody End Sub