Create a plaintext email message for sending later (store it in the Outbox folder) using default email account and identity

This is an archived page. It's not actively maintained.

let am = MailServices.accounts;

// Set the data of the message
let compFields = Components.classes[";1"].createInstance(Components.interfaces.nsIMsgCompFields);
compFields.from =; // f.e. "" or .identityName f.e "Name Surname <>" = "";
compFields.subject = "test";
compFields.body = "message body";

/* Warning about body content
If there was NO attachment, the composeFields didn't know where the body terminated, so when asked to send the message Thunderbird
didnt know where the message terminated and therefore received a timeout error from the smtp server, f.e.

Send Message Error
Sending of message failed.
The message could not be sent because the connection to SMTP server timed out. Try again or contact your network

So without attachment(s) body content need \r\n at the end, f.e. ".body = "message body\r\n"

If there was an attachment being added, the composeFields instance properly terminates the body with a '\n', adds the attachment and
therefore would properly send the message out, f.e. ".body = "message body".

// trick: if there is no attachment(s)
   // correct body to prevent throw SMTP error
   compFields.body = compFields.body + "\r\n";

let msgComposeParams = Components.classes[";1"].createInstance(Components.interfaces.nsIMsgComposeParams);
msgComposeParams.composeFields = compFields;

let gMsgCompose = Components.classes[";1"].createInstance(Components.interfaces.nsIMsgCompose);


/* definiton at
void nsIMsgCompose::SendMsg(
   in MSG_DeliverMode  deliverMode,
   in nsIMsgIdentity     identity,
   in string     accountKey,
   in nsIMsgWindow   aMsgWindow,
   in nsIMsgProgress     progress

                    am.defaultAccount.defaultIdentity, // identity
                    am.defaultAccount.key, // account, f.e. account4
                    null, // message window
                    null); // nsIMsgProgress