How to get the content of selected message?

Talk about add-ons and extension development.
Post Reply
haiying
Posts: 2
Joined: June 3rd, 2005, 6:24 am

How to get the content of selected message?

Post by haiying »

Hi all,

I am developing an extension. I hope to get every part of a selected message. Now I can get the subject of the message, but I do not know how to get the content(body) of the message. Can anybody help me?

Thanks in advance,
Haiying
Old Ausdilecce
Posts: 0
Joined: December 31st, 1969, 5:00 pm

Post by Old Ausdilecce »

AFAIK, you cannot get the content of the message unless you physically read the underlying file..

You can do this easily enough :)

the nsIMsgDBHdr gives you
messageOffset - the character position in the underlying file where the message starts
messageSize - the number of characters the message uses in the file
folder.path.nativePath - the native path of the underlying file..

so just open the file using native path, and grab messageSize characters after messageOffset from the file...

here's a macro that illustrates
<code>
// this will populate the msgContent var with the text of the message..
// a message MUST be selected in TBs main window..
var dbv = GetDBView(); // get the gDBView global var
var hdr = dbv.hdrForFirstSelectedMessage;
var offset = hdr.messageOffset; // this will be what character the message starts in the file
var siz = hdr.messageSize; // this will be the number of characters the message is
var fil = hdr.folder.path.nativePath; // this will be the native path to the file
var sfile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
sfile.initWithPath(fil);
var fileInputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
fileInputStream.init( sfile, 0x01, 0444, null ); // read
var fileScriptableIO = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
fileScriptableIO.init(fileInputStream);
var msgContent = ''
msgContent = fileScriptableIO.read( offset + siz ); // we could get the whole file by using sfile.fileSize
// but lets just get up to the end of the message we want
fileScriptableIO.close();
fileInputStream.close();
msgContent = msgContent.substr(offset,siz);
alert(msgContent);

</code>
User avatar
jensb
Posts: 544
Joined: April 23rd, 2003, 12:42 pm
Location: Germany
Contact:

Post by jensb »

There's an easier method - you can read directly from the message service, without fiddling with offsets and the file directly. The following was posted at http://forums.mozillazine.org/viewtopic.php?t=214824 - I bookmarked it, but have not yet tested it myself.
sumanraj wrote:var content;
var MessageURI = GetFirstSelectedMessage();
var MsgService = messenger.messageServiceFromURI(uri);
var MsgStream = Components.classes["@mozilla.org/network/sync-stream-listener;1"].createInstance();
var MsgStrem_Inputstream = MsgStream.QueryInterface(Components.interfaces.nsIInputStream);
var ScriptInput = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance();
var ScriptInputStream = ScriptInput .QueryInterface(Components.interfaces.nsIScriptableInputStream);
ScriptInputStream .init(consumer);
try {
MsgService .streamMessage(MessageURI,MsgStream, msgWindow, null, false, null);
} catch (ex) {
return;
}
ScriptInputStream .available();
while (ScriptInputStream .available()) {
content = content + ScriptInputStream .read(512);
}
alert(content);


With both methods, you'd still have to actually parse the message (which is non-trivial in case of multi-part MIME messages) to extract the body.
Mouse Gestures - control your browser the elegant way
MessageFaces - embed pictures in mail header
haiying
Posts: 2
Joined: June 3rd, 2005, 6:24 am

Post by haiying »

Thanks a lot, ausdilecce and jensb.

Haiying
bbondy
Posts: 8
Joined: July 20th, 2006, 9:54 am
Location: Windsor, Ontario, Canada
Contact:

There are a couple of errors in the previous post

Post by bbondy »

There are a couple of errors in the previous post.
Here is the correct code to obtain the message body of the selected message:

var content;
var MessageURI = GetFirstSelectedMessage();
var MsgService = messenger.messageServiceFromURI(MessageURI);
var MsgStream = Components.classes["@mozilla.org/network/sync-stream-listener;1"].createInstance();
var MsgStrem_Inputstream = MsgStream.QueryInterface(Components.interfaces.nsIInputStream);
var ScriptInput = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance();
var ScriptInputStream = ScriptInput .QueryInterface(Components.interfaces.nsIScriptableInputStream);
ScriptInputStream .init(MsgStream);
try
{
MsgService .streamMessage(MessageURI,MsgStream, msgWindow, null, false, null);
}
catch (ex)
{
return;
}
ScriptInputStream .available();
while (ScriptInputStream .available())
{
content = content + ScriptInputStream .read(512);
}
alert(content);

Thanks,
Brian R. Bondy
http://www.brianbondy.com
adeliam2839
New Member
Posts: 1
Joined: July 23rd, 2011, 4:18 pm

Re: How to get the content of selected message?

Post by adeliam2839 »

Just checking to see if this is java code? Also, can I use this for any database or just mozilla?
bbondy
Posts: 8
Joined: July 20th, 2006, 9:54 am
Location: Windsor, Ontario, Canada
Contact:

Re: How to get the content of selected message?

Post by bbondy »

adeliam2839 it is Javascript but I suspect this is not related to what you are looking for.
Post Reply