Most operations on messages expect you to have an nsIMsgDBHdr
; if all you have is a URI/URL, the first thing you'll need to do is to look up the header. The easiest way to do this, which should work for all kinds of messages (messages in folders, attached messages, and .eml files), is the following:
// In some windows, this already exists as a global variable (usually
// named `gMessenger`).
var messenger = Cc["@mozilla.org/messenger;1"]
.createInstance(Ci.nsIMessenger);
var msgHdr = messenger.msgHdrFromURI(messageURI);
However, there are other ways to get the message header for specific kinds of URIs. If you have a URI beginning with mailbox-message://
, imap-message://
, or news-message://
, the following should work:
var msgHdr = messenger.messageServiceFromURI(messageURI)
.messageURIToMsgHdr(messageURI);
If your URI is an nsIURI object, and uri.spec
begins with mailbox://
, imap://
, or news://
, you can get the header directly from your URI:
// If you have a URI as a string, create an nsIURI object first: Cu.import("resource://gre/modules/Services.jsm"); var uri = Services.io.newURI(uriString, null, null); // Now get the message header. var msgHdr = uri.QueryInterface(Components.interfaces.nsIMsgMessageUrl) .messageHeader;
List Attachments
This is a quick recipe for getting a list of attachments present in a MIME message. This uses the new MimeMessage
representation for multipart email messages. Technically speaking, the code belongs to Gloda, but it doesn't use the global database. It simply is a convenience layer on top of libmime that allows you to get a hierarchical representation of an email, manipulate it, examinate it, without having to abide by the streaming pattern enforced by libmime.
If you have a nsIMsgDbHdr
, you should first obtain a MimeMessage
by calling MsgHdrToMimeMessage
first. The function is defined here and you have all the documentation written. Examples can be found by a dxr search.
This function returns a list of MimeMessageAttachment
s. This class is defined in http://dxr.mozilla.org/comm-central/source/mailnews/db/gloda/modules/mimemsg.js, some of the useful attributes of a MimeMessageAttachment
are: contentType
, name
(the filename of the attachment). Starting with Thunderbird 3.3, there's also a size
property. use nsIMessenger.formatFileSize
to get a nice human-readable size.
Get Headers
Some header entries are available in msgHdr, some are not. Those can be found in the mimemessage returned by the callback in MsgHdrToMimeMessage
. They can be retrieved by aMimeMsg.get('in-reply-to') or aMimeMsg.headers['in-reply-to'][0] (if, for example, the in-reply-to header is required - note the [0] if aMimeMsg.headers is used).