0

I work on a thunderbird addon and a need to send a mail written in by the addon and not by the user.

I try to do this with that but is doesn't work :

var am = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager);
var cf = Components.classes["@mozilla.org/messengercompose/composefields;1"].createInstance(Components.interfaces.nsIMsgCompFields);
cf.from = "somebody@somewhere.com";
cf.to = "somebodyelse@somewhere.com";
cf.subject = "test";
cf.body = "just a test\r\n";

var params = Components.classes["@mozilla.org/messengercompose/composeparams;1"].createInstance(Components.interfaces.nsIMsgComposeParams);
params.composeFields = cf;

var msgSend = Components.classes["@mozilla.org/messengercompose/send;1"].createInstance(Components.interfaces.nsIMsgSend);

var msgCompose = Components.classes["@mozilla.org/messengercompose/compose;1"].createInstance(Components.interfaces.nsIMsgCompose);
msgCompose.compFields = cf;
msgCompose.initialize(window,params); //[Exception... "Could not convert JavaScript argument arg 0 [nsIMsgCompose.initialize]"  nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)"  location: "JS frame :: *****"  data: no]

msgCompose.SendMsg(msgSend.nsMsgDeliverNow, am.defaultAccount.defaultIdentity, am.defaultAccount.key, null, null);

Anyone know where is my mistake or a simple method to do that ?

Thanks

Jeff
  • 11
  • 3

1 Answers1

1

Sending an email from an addon is a hauntingly complex task. Fortunately, libraries exist, namely in the form of thunderbird-stdlib. See https://github.com/protz/thunderbird-stdlib/blob/master/send.js#L228 for the implementation of the sendMessage function, https://github.com/protz/GMail-Conversation-View/blob/master/content/stub.compose-ui.js#L879 for an example of how to use the function.

Jonathan Protzenko
  • 1,709
  • 8
  • 13