D.Bugger
D.Bugger

Reputation: 2359

LotusScript: Combine two NotesMIMEEntity objects

I wrote some code to create and send a new mail using MIME objects. That works, no problems here.

Now I need to add the Body of an existing document. That Body field is in MIME as well. What is the correct way to add this NotesMIMEEntity object to the mail? Can I combine two NotesMIMEntity objects? Can I maybe attach one object to another?

Thanks for your help!

Update

This is what I tried. The object item contains the MIMEEntity from the other document. Object body is the new MIMEEntity under construction. The result of the code is zip, nada...

Set bodyChild= body.createChildEntity()
Set stream= session.Createstream()
Call item.Getcontentasbytes(stream)
Call bodyChild.Setcontentfrombytes(stream, {text/html;charset="iso-8859-1"}, ENC_NONE)
Call stream.Close()

Upvotes: 0

Views: 978

Answers (2)

togusa75
togusa75

Reputation: 31

I only wanted to comment on D.Bugger solution, which is great and works with no problems.

I wanted to add that if some child entities are file attachments, you need to use the following sintax:

Call childTo.createHeader( mimeHeader.HeaderName ).Setheadervalandparams( mimeHeader.Getheadervalandparams() )

Because "the params" in the "Content-Type" and "Content-Disposition" headers contain meaningful information, namely the file attachment filename.

Also you should add at least another header to the ones you get from the childs, which is "Content-Type".

Otherwise the file attachment child entities are copied, but they'll lack a file name and type and therefore will be useless.

The complete corrected Do loop is here:

Do Until childFrom Is Nothing
    Set childTo= mimeTo.createChildEntity()

    Set mimeHeader= childFrom.GetNthHeader("Content-Type")

    If Not mimeHeader Is Nothing Then
        Call childTo.createHeader( mimeHeader.HeaderName ).Setheadervalandparams( mimeHeader.Getheadervalandparams() )
    End If

    Set mimeHeader= childFrom.GetNthHeader("Content-Disposition")

    If Not mimeHeader Is Nothing Then
        Call childTo.createHeader( mimeHeader.HeaderName ).Setheadervalandparams( mimeHeader.Getheadervalandparams() )
    End If

    Set mimeHeader= childFrom.GetNthHeader("Content-ID")

    If Not mimeHeader Is Nothing Then
        Call childTo.createHeader( mimeHeader.HeaderName ).Setheaderval( mimeHeader.Getheaderval() )
    End If

    Call CopyMultipartMime(childFrom, childTo)

    Set childFrom = childFrom.GetNextSibling
Loop

Apart from this, thanks for your answer and sorry if I posted an answer, but I cannot comment on yours, apparently.

Upvotes: 1

D.Bugger
D.Bugger

Reputation: 2359

Here's the code that copies text, images and some headers to a target MIME entity. I hope someone else can use it too. Thanks all!

Sub CopyMultipartMime (mimeFrom As NotesMIMEEntity, mimeTo As NotesMIMEEntity) 
    '** recursively get all the parts of a multi-part MIME entity
    Dim childFrom As NotesMIMEEntity
    Dim childTo As NotesMIMEEntity
    Dim stream As NotesStream
    Dim mimeHeader As NotesMIMEHeader
    Dim childHeader As NotesMIMEHeader

    On Error GoTo catch

    count=count+1           
    If count>1000 Then Exit Sub ' failsafe

    Set stream= session.Createstream()      
        Call mimeFrom.getContentAsBytes(stream)
        Call mimeTo.setContentFromBytes(stream, mimeFrom.Contenttype + "/" + mimeFrom.Contentsubtype, mimeFrom.Encoding)
    Call stream.Close()
    Set childFrom = mimeFrom.GetFirstChildEntity
    Do Until childFrom Is Nothing
        Set childTo= mimeTo.createChildEntity()
        Set mimeHeader= childFrom.GetNthHeader("Content-Disposition")
        If Not mimeHeader Is Nothing Then
            Call childTo.createHeader(mimeHeader.HeaderName).Setheaderval(mimeHeader.GetHeaderVal())
        End If
        Set mimeHeader= childFrom.GetNthHeader("Content-ID")
        If Not mimeHeader Is Nothing Then
            Call childTo.createHeader(mimeHeader.HeaderName).Setheaderval(mimeHeader.GetHeaderVal())
        End If
        Call CopyMultipartMime(childFrom, childTo) 
        Set childFrom = childFrom.GetNextSibling
    Loop    
    Exit Sub
catch:
    Error Err, Error$ & ", " & GetThreadInfo(1) & " line " & Erl        
End Sub

Upvotes: 2

Related Questions