0
votes

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()
2
Much as I dislike the downvote, I'd love to know the reason behind it. Is this such a stupid question? And if it is, please tell me why, for I really don't know how to do it. By the way, as they say: "There are no stupid questions..." - D.Bugger
Making progress, using GetMultipartMime function from cdn.ttgtmedia.com/searchDomino/downloads/Exportemail.txt - D.Bugger
Have you considered trying to use GetContentAsBytes for both NoteMIMEEntity objects, writing both NotesStreams to byte arrays, combining the byte arrays, reading the combined array into a NotesStream, and then using NotesMIMEEntity.SetContentFromBytes to read the combined stream? - Richard Schwartz
P.S. Not my downvote. - Richard Schwartz
I assume that GetContentAsBytes or Text only gets the content of the current entity, and not of its children. Using GetMultipartMime gives me quite a lot of text, but the structure isn't quite right, yet. Woking on this. Thanks for your assistance! - D.Bugger

2 Answers

2
votes

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
1
votes

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.