How to display the body of an email with a thunderbird-addon?

585 views Asked by At

This is my code :

var newMailListener = {
 msgAdded: function(aMsgHdr) {
  if(!aMsgHdr.isRead) {
   gFolderDisplay.selectMessage(aMsgHdr); 
   var uri = gFolderDisplay.selectedMessageUris;
   alert(uri);
   msgHdr = messenger.messageServiceFromURI(uri).messageURIToMsgHdr(uri);
   alert(getMessageBody(msgHdr,uri));
   goDoCommand("cmd_markAsRead");
  }
 }
};
function init() {
 var ancienmsg = null;
 var notificationService = Components.classes["@mozilla.org/messenger/msgnotificationservice;1"]
.getService(Components.interfaces.nsIMsgFolderNotificationService);
 notificationService.addListener(newMailListener, notificationService.msgAdded);
}
addEventListener("load", init, true);

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

It is supposed, according to the mozilla's documentation, to display, in an alert, the body of the mail received. But, every time during the return of the getMessageBody method, thunderbird crash and I need to restart it. Does anybody have an idea of why and how to display it ?

1

There are 1 answers

0
Victor Bernard On BEST ANSWER

In another Stackoverflow question, I find this and it works for me.

Components.utils.import("resource:///modules/gloda/mimemsg.js");
var newMailListener = {
 msgAdded: function(aMsgHdr) {
  if( !aMsgHdr.isRead ){
   MsgHdrToMimeMessage(aMsgHdr, null, function (aMsgHdr, aMimeMessage) {
    // do something with aMimeMessage:
    alert("the message body : " + aMimeMessage.coerceBodyToPlaintext());

    //alert(aMimeMessage.allUserAttachments.length);
    //alert(aMimeMessage.size);
   }, true);
  }
 }
};

But you only get the text and not the HTML.