如何使用thunderbird-addon显示电子邮件正文?

时间:2017-01-06 17:45:15

标签: javascript email thunderbird-addon

这是我的代码:

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,  
                                 { });  
}

根据mozilla的文档,可以在警报中显示收到的邮件正文。但是,每次返回getMessageBody方法时,thunderbird崩溃都需要重新启动它。有没有人知道为什么以及如何展示它?

1 个答案:

答案 0 :(得分:0)

在另一个Stackoverflow问题中,我发现了这个问题,它对我有用。

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);
  }
 }
};

但是你只得到文本而不是HTML。