使用Thunderbird源保存电子邮件附件

时间:2013-04-08 12:41:52

标签: javascript thunderbird thunderbird-addon

我想使用thunderbird源代码保存电子邮件附件。我在您的论坛中获得了以下代码..但它无法正常工作

    alert("Messages selected: " + gFolderDisplay.selectedCount);
      let enumerator = gFolderDisplay.selectedMessages;
        for each (let msgHdr in fixIterator(enumerator, Ci.nsIMsgDBHdr)) {
          var messageID = msgHdr.messageId;
           alert("MessageID: " + messageID);
           var subject = msgHdr.mime2DecodedSubject;
          alert("Subject: " + subject);
            MsgHdrToMimeMessage(msgHdr, null, function (aMsgHdr, aMimeMsg) {
              try {
        alert("Size of the message: " + aMimeMsg.size);
            alert("Structure of the message:\n" + aMimeMsg.prettyString(true, undefined, true));
             let attachments = aMimeMsg.allUserAttachments || aMimeMsg.allAttachments;
             alert("Number of attachments: " + attachments.length);
                   for (let [index, att] in Iterator(attachments))
        {
            alert ("URL: " + att.url  + " Name: " + att.name);
            let ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);  
            let neckoURL = null;  
            neckoURL = ioService.newURI(att.url, null, null);  
            neckoURL.QueryInterface(Ci.nsIMsgMessageUrl);  
            let uri = neckoURL.uri;  
            let attInfo = new AttachmentInfo(att.contentType, att.url, att.name, uri, att.isExternal);
            attInfo.save();
        }
    } catch (err) {
        alert(err);
    }
}, true, { examineEncryptedParts: true, });
}

使用上面的代码,我可以浏览所选的邮件。但不保存附件。显示附件数量。 而且我想为所选邮件设置我自己的标签。我怎么能实现这个目标?请帮帮我...提前致谢

1 个答案:

答案 0 :(得分:0)

工作代码

      var enumerator = gFolderDisplay.selectedMessages;
        for each (var msgHdr in fixIterator(enumerator, Components.interfaces.nsIMsgDBHdr)) {
        msgHdr.setStringProperty("docuHive","dhivelabel");

            MsgHdrToMimeMessage(msgHdr, null, function (aMsgHdr, aMimeMsg) {
              try {
             var attachments = aMimeMsg.allUserAttachments || aMimeMsg.allAttachments;
                   for (var [index, att] in Iterator(attachments))
        {
            var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);  
            var neckoURL = null;  
            neckoURL = ioService.newURI(att.url, null, null);  
            neckoURL.QueryInterface(Components.interfaces.nsIMsgMessageUrl);  
            var uri = neckoURL.uri;  
            var attInfo = new AttachmentInfo(att.contentType, att.url, att.name, uri, att.isExternal); 
            // getting the chrome directory
            var file = Components.classes["@mozilla.org/file/directory_service;1"].
           getService(Components.interfaces.nsIProperties).
           get("AChrom", Components.interfaces.nsIFile);

           var msguri = msgHdr.folder.getUriForMsg(msgHdr);

    messenger = Components.classes["@mozilla.org/messenger;1"]
                       .createInstance(Components.interfaces.nsIMessenger);
                       alert(messenger);
        messenger.saveAttachmentToFolder(att.contentType,att.url,"attachmentname.extension",msguri,file);

        }
    } catch (err) {
        alert(err);
    }
}, true, { examineEncryptedParts: true, });
}