get message body using javascript (thunderbird)

1.7k views Asked by At

I am developing a thunderbird extension and I want to get message body of received message.
I used this function but it didnt work... How can I achieve this?

 function getMessageBody() {

     let messageURI = GetLoadedMessage();

     let aMessageHeader = messenger.msgHdrFromURI(messageURI);

     // I try 
     // aMessageHeader = messenger.messageServiceFromURI(messageURI).messageURIToMsgHdr(messageURI); 
     // aMessageHeader = url.QueryInterface(Components.interfaces.nsIMsgMessageUrl).messageHeader;  
     // too

     let messenger = Components.classes["@mozilla.org/messenger;1"]
         .createInstance(Components.interfaces.nsIMessenger);
     let listener = Components.classes["@mozilla.org/network/sync-stream-listener;1"]
         .createInstance(Components.interfaces.nsISyncStreamListener);
     let uri = aMessageHeader.folder.getUriForMsg(aMessageHeader);
     messenger.messageServiceFromURI(uri)
         .streamMessage(uri, listener, null, null, false, "");
     let folder = aMessageHeader.folder;
     return folder.getMsgTextFromStream(listener.inputStream,
         aMessageHeader.Charset,
         65536,
         32768,
         false,
         true, {});
 }
1

There are 1 answers

0
Dileepa Jayakody On

Are you calling getMessageBody() in a mailListener which intercepts new incoming emails? In that case I have the same problem, where the message to stream is not yet available inside the mailListener's msgAdded method. I think it's because nsISyncStreamListener uses a blocking stream. see my question : How to intercept incoming email and retrieve message body in thunderbird

If you are trying to fetch the message body of already available mails in a particular "folder" (instead of intercepting new incoming emails), then you need to get hold of the nsIMsgDBHdr first and then get the uri from it to stream the body. Following code worked for me, where I iterate all the messages of a particular folder.

for each (let aMessageHeader in fixIterator(folder.msgDatabase.EnumerateMessages(), Ci.nsIMsgDBHdr)){

                let messenger = Components.classes["@mozilla.org/messenger;1"]
                                .createInstance(Components.interfaces.nsIMessenger);
                    let listener = Components.classes["@mozilla.org/network/sync-stream-listener;1"]
                                    .createInstance(Components.interfaces.nsISyncStreamListener);
                    let uri = aMessageHeader.folder.getUriForMsg(aMessageHeader);
                    messenger.messageServiceFromURI(uri).streamMessage(uri, listener, null, null, false, "");
                    let folder = aMessageHeader.folder;
                    let messageBody = folder.getMsgTextFromStream(listener.inputStream,
                                       aMessageHeader.Charset,
                                       65536,
                                       32768,
                                       false,
                                       true,
                                       { });
                    alert("message body : " + messageBody); 
}

hope this helps!