Skip to content
Kai edited this page Mar 17, 2014 · 5 revisions

List all identites of all accounts

Two things to produce code, that ist better to read:

  1. Use MailServices to access services like the nsIMsgAccountManager. You have to import resource:///modules/mailServices.js to use this.
  2. Use fixIterator to iterate over mozillas custom arrays like nsISupportsArray. You have to import resource:///modules/iteratorUtils.jsm to use this.
Components.utils.import("resource:///modules/mailServices.js");
Components.utils.import("resource:///modules/iteratorUtils.jsm");

var accounts = MailServices.accounts.accounts;
// Old way: var accounts = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager).accounts;

for each (let thisAccount in fixIterator(accounts, Components.interfaces.nsIMsgAccount)) {	
	for each (let thisIdentity in fixIterator(thisAccount.identities, Components.interfaces.nsIMsgIdentity)) {
	    Application.console.log(thisIdentity.email);
	}
	/* old way:
	for (var j = 0; j < thisAccount.identities.length; j++) {
		var thisIdentity = thisAccount.identities.queryElementAt(j, Components.interfaces.nsIMsgIdentity);
		Application.console.log(thisIdentity.email);
	}
	*/
}

Quellen: