All MIMEs little children
Nov 14, 2012
http://www.bleedyellow.com/blogs/DominoHerald/entry/all_mimes_little_children?lang=en_us
I've used a NotesStream to put a file in a Rich Text Field or MIME quite a number of times over the last few years. Recently, I've needed to expand on this and put multiple attachments in a field. I didn't find much on this, so I thought I'd post. The important trick seems to be setting a ParentEntity. In your typical MIMEEntity, which is "body" in all the examples (including this one) set body as a parent entity. Then create child entities for each file, and stream it in. Here is the LotusScript code:
    session.ConvertMime = False    
    Dim parent As NotesMIMEEntity
    Set body = streamDoc.CreateMIMEEntity("body")    
    Set header = body.CreateHeader("Content-Type")
    Call header.SetHeaderVal("multi-part/mixed")
    Set parent = body.CreateParentEntity
    
    Set child = body.CreateChildEntity
    Dim nStream2 As NotesStream
    Set nStream2 = session.CreateStream
    nStream2.WriteText |Here is placeholder text|
    child.SetContentFromText nStream2, "text/html", ENC_NONE    
    
    If Not nStream.Open("C:\images\DominoJava.jpg") Then
        Print "Open failed"
    Else   
        Set child = parent.CreateChildEntity
        Set header = child.CreateHeader("Content-Disposition")
        Call header.SetHeaderValAndParams(|attachment; filename="DominoJava.jpg"|)
        Call child.SetContentFromBytes(nStream, "image/jpeg", ENC_IDENTITY_BINARY)
        Call nStream.close
        Call nStream.Truncate()   
       
        nStream.Open("C:\images\shop.jpg")
        Set child = parent.CreateChildEntity  'you can just reuse the child
        Set header = child.CreateHeader("Content-Disposition")
        Call header.SetHeaderValAndParams(|attachment; filename="shop.jpg"|)
        Call child.SetContentFromBytes(nStream, "image/jpeg", ENC_IDENTITY_BINARY)
        Call nStream.close
        Call nStream.Truncate()    
       
        Call streamDoc.Save(True, False)
        Call nStream.Close
    End if
       session.ConvertMime = True   

 
On thing is important - putting some text (even "" will work) in the first child, the one with the placeholder text above. Otherwise you get a .dat file in your field. If someone has another way to prevent that, I'd love to know it.
Here is the code in Java:
   
            
session.setConvertMIME(false);
                      Stream stream = session.createStream();
                            allObjects.addElement(stream);
                            int ct = 0;
                            MIMEEntity body;                              
                            if(doc.hasItem("body")){                           
                                body = doc.getMIMEEntity("body");
                            } else {                              
                                body = doc.createMIMEEntity();
                            }                                                
                            allObjects.addElement(body);
                            MIMEEntity parent = body.createParentEntity();
                            allObjects.addElement(parent);
                            MIMEHeader header = body.createHeader("Content-Type");                         
                            allObjects.addElement(header);
                            header.setHeaderVal("multi-part/mixed");
                            MIMEEntity child = body.createChildEntity();
                            Stream tempStream = session.createStream();
                            allObjects.addElement(tempStream);
                            tempStream.writeText("");
                            child.setContentFromText(tempStream, "text/html", MIMEEntity.ENC_NONE);
                            String[] temp;
                            String delimiter = "%";
                            /* given string will be split by the argument delimiter provided. */
                            temp = rtiText.split(delimiter);
                            for(int i =0; i < temp.length ; i++) {
                                    if (temp[i].length()> 10) {
                                    byte[] decoded = Base64.decodeBase64(temp[i]);        
                                    ct++;
                                    String imageBaseName = doc.getItemValueString("pdfBaseImageName");
                                    System.out.println("Image base: "+  imageBaseName + ct);
                                    stream.open(imageBaseName + ct + ".png");
                                    stream.write(decoded);       
                                    child = parent.createChildEntity();
                                    header = child.createHeader("Content-Disposition");
                                    header.setHeaderValAndParams("attachment;filename=\"" + imageBaseName + ct + ".png\"");
                                    child.setContentFromBytes(stream, "image/png", MIMEEntity.ENC_IDENTITY_BINARY);
                                    stream.close();
                                    stream.truncate();
                                }
                            }
           session.setConvertMIME(true);

                        
                         
                This usesa
 base64 decoder from the great Julian Robichaux  since I'm getting a base64 string in and I need to decode it so it's a real attachment when the user sees it. Hopefully someone will find this useful.
Cheers,
Brian
This is an archive of my previous blog http://www.bleedyellow.com/blogs/DominoHerald attachments are in the download control