Account examples

Draft
This page is not complete.

This content covers features introduced in Thunderbird 3

This article provides examples on accessing and manipulating Thunderbird accounts. Account interfaces provides an overview of the related interfaces. See An overview of Thunderbird components for a general description of the Thunderbird user interface and related programmatic interfaces.

Iterate over all known accounts

var acctMgr = Components.classes["@mozilla.org/messenger/account-manager;1"]
                        .getService(Components.interfaces.nsIMsgAccountManager);
var accounts = acctMgr.accounts;
if (accounts.queryElementAt) {
  // Gecko 17+
  for (var i = 0; i < accounts.length; i++) {
    var account = accounts.queryElementAt(i, Components.interfaces.nsIMsgAccount);
    // Do something with account
  }
} else {
  // Gecko < 17
  for (var i = 0; i < accounts.Count(); i++) {
    var account = accounts.QueryElementAt(i, Components.interfaces.nsIMsgAccount);
    // Do something with account
  }
}

Introspect the details of particular accounts (names, parameters, etc.)

var acctMgr = Components.classes["@mozilla.org/messenger/account-manager;1"]
                        .getService(Components.interfaces.nsIMsgAccountManager);
var accounts = acctMgr.accounts;
for (var i = 0; i < accounts.length; i++) {
  var account = accounts.queryElementAt(i, Components.interfaces.nsIMsgAccount);
  Application.console.log(account.key);
  // account.incomingServer is an nsIMsgIncomingServer
  // account.identities is an nsISupportsArray of nsIMsgIdentity objects
  //                    you can loop through it just like acctMgr.accounts above
  // account.defaultIdentity is an nsIMsgIdentity
}

Iterate over the folders in an account

var acctMgr = Components.classes["@mozilla.org/messenger/account-manager;1"]
                        .getService(Components.interfaces.nsIMsgAccountManager);
var accounts = acctMgr.accounts;
for (var i = 0; i < accounts.length; i++) {
  var account = accounts.queryElementAt(i, Components.interfaces.nsIMsgAccount);
  var rootFolder = account.incomingServer.rootFolder; // nsIMsgFolder
  Application.console.log(rootFolder.prettiestName);
  if (rootFolder.hasSubFolders) {
    var subFolders = rootFolder.subFolders; // nsIMsgFolder
    while(subFolders.hasMoreElements()) {
      var folder = subFolders.getNext().QueryInterface(Components.interfaces.nsIMsgFolder);
      Application.console.log(folder.prettiestName);
    }
  }
}

Document Tags and Contributors

 Contributors to this page: chrisdavidmills, Oeekker, trevorh, jenzed, Clarkbw, Jcranmer
 Last updated by: chrisdavidmills,