首页 > 解决方案 > Lotusscript:检索电子邮件正文中的图像

问题描述

在我的代理中,我尝试检索当前电子邮件中的所有文件。我附加的代码工作正常,除了电子邮件正文中的图像。我设法检索到附加到电子邮件的所有文件和图像,除了复制并粘贴在电子邮件文本中间的图片。这是我的代码:

    Dim session As New NotesSession
    Dim doc As NotesDocument
    Dim db As NotesDatabase
    Dim item As Variant
    Dim CurrentDocColl As NotesDocumentCollection

    Set db = Session.Currentdatabase
    Set CurrentDocColl = db.Unprocesseddocuments
    Set doc = CurrentDocColl.Getfirstdocument
    While Not doc Is Nothing
        Set item = doc.GETFIRSTITEM("Body")
        If doc.HasEmbedded Then
            ForAll attachment In item.EmbeddedObjects
                Call attachment.ExtractFile (pathname & "\" & attachment.Name)
            End ForAll
        End If
        Set doc=CurrentDocColl.Getnextdocument(doc)
    Wend

如何检索这些图像?

非常感谢您的帮助

标签: lotus-noteslotus-dominolotusscript

解决方案


我有一个代理做了很多,但它并不短。您需要做的是通过 XML DomParser 运行文档,沿着 DOM 树向下走,当您找到名称中包含“JPEG”或“PNG”的节点(内联图像本身)时,将数据流式传输到文件并保存。该代码是我在网上找到的代理(我再也找不到了,否则我会给予信任)和我所做的工作的组合。您将无法复制/粘贴此示例代码并期望它能够工作,为简洁起见,我删除了一些内容(如声明变量和支持函数)。

    Sub Initialize
        Dim dxlExp As NotesDXLExporter
        Set dxlExp = s.CreateDXLExporter
        Call dxlExp.setInput(Doc)
        Set DomParser=s.CreateDOMparser()
        Call DomParser.Setinput(dxlExp)
        Dim dxlImp As NotesDXLImporter
        Set dxlImp = s.Createdxlimporter()
        Call dxlImp.Setinput(domParser)
        Call dxlImp.SetOutput(db)
        On Event PostDomParse From DomParser Call DomInputProcessed
        Call dxlExp.Process
    End Sub
    
    Sub DomInputProcessed(DomParser As NotesDomParser)
        Dim DomNode As NotesDomNode
        Set DomNode = DomParser.Document
        Call walkTree(DomParser, DomNode)
        Exit Sub
    End Sub
    
    Sub walkTree (DomParser As NotesDOMParser, node As NotesDOMNode)
         Select Case node.NodeType
         Case DOMNODETYPE_DOCUMENT_NODE:        ' If it is a Document node
             domParser.Output( "<?xml version='1.0' encoding='utf-8'?>"+LF )
    
             Set child = node.FirstChild   ' Get the first node
             Dim numChildNodes As Integer
             numChildNodes = node.NumberOfChildNodes
    
             While numChildNodes > 0 
                 Set child = child.NextSibling ' Get next node
                 numChildNodes = numChildNodes - 1                  
                 Call walkTree(DOMParser, child)
             Wend
    
         Case DOMNODETYPE_DOCUMENTTYPE_NODE:   ' It is a <!DOCTYPE> tag
             domParser.Output("<!DOCTYPE "+ node.NodeName+ ">" + LF)
    
         Case DOMNODETYPE_TEXT_NODE:           ' Plain text node
             value = xmlReplace(node.NodeValue)
             domParser.Output(value)
    
         Case DOMNODETYPE_ELEMENT_NODE:        ' Most nodes are Elements
        Select Case node.NodeName
            Case "jpeg"
                 Dim jpegfile As String 
                 ' Step 1, write the MIME file
                 Dim base64node As NotesDOMNode
                 Set base64Node = node.Firstchild
                 Dim base64Out As NotesStream
                 Set base64Out = s.createStream()
                 Dim bytesWritten As Long
                 bytesWritten = base64Out.Writetext(base64Node.NodeValue)
                 ' Step 2, Read the MIME file and decode it.
                 Set db=s.currentdatabase
                 Set doc=db.createDocument()
                 Set m=doc.Createmimeentity("Image1")
                 Call m.setContentFromText(base64Out, "image/jpeg", 1727)
                 Call m.Decodecontent()
                 Dim JPEGOut As NotesStream
                 Set JPEGOut = s.createStream()
                 jpegFile = RandomFileName(baseDir, ".jpg")
                 JPEGOut.open(jpegFile)
                 Call m.Getcontentasbytes(JPEGOut, True)
                 Call JPEGOut.Close()
                 attachmentNamesStr = attachmentNamesStr + jpegFile + "~"  
                 ' Step 3, remove the jpeg and its child node
                 ' We do this by just not sending anything to the DomParser output.

           Case "png"
      ' Same as JPEG except it's PNG.

    
        End Select
    End Select  'node.NodeType
    End If        'Not node.IsNull
End Sub 

推荐阅读