Account examples

  • Revision slug: Thunderbird/Account_examples
  • Revision title: Account examples
  • Revision id: 130286
  • Created:
  • Creator: jenzed
  • Is current revision? No
  • Comment no wording changes

Revision Content

{{ draft() }}

{{ tb_minversion_header("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;
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.Count(); 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.Count(); 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);
}
}
}

Revision Source

<p>{{ draft() }}</p>
<p>{{ tb_minversion_header("3") }}</p>
<p>This article provides examples on accessing and manipulating Thunderbird accounts. <a class="internal" href="/en/Thunderbird/Account_interfaces" title="en/Thunderbird/Account interfaces">Account interfaces</a> provides an overview of the related interfaces. See <a class="internal" href="/En/Extensions/Thunderbird/An_overview_of_the_Thunderbird_interface" title="En/Extensions/Thunderbird/An overview of the Thunderbird interface">An overview of Thunderbird components </a>for a general description of the Thunderbird user interface and related programmatic interfaces.</p>
<h2 id="Iterate_over_all_known_accounts">Iterate over all known accounts</h2>
<pre class="brush: js"><code>var acctMgr = Components.classes["@mozilla.org/messenger/account-manager;1"]<br>                        .getService(Components.interfaces.nsIMsgAccountManager);<br>var accounts = acctMgr.accounts;<br>for (var i = 0; i &lt; accounts.Count(); i++) {<br>  var account = accounts.QueryElementAt(i, Components.interfaces.nsIMsgAccount);<br>  // Do something with account<br>}</code></pre>
<h2 id="Introspect_the_details_of_particular_accounts_(names,_parameters,_etc.)">Introspect the details of particular accounts (names, parameters, etc.)</h2>
<pre class="brush: js"><code>var acctMgr = Components.classes["@mozilla.org/messenger/account-manager;1"]<br>                        .getService(Components.interfaces.nsIMsgAccountManager);<br>var accounts = acctMgr.accounts;<br>for (var i = 0; i &lt; accounts.Count(); i++) {<br>  var account = accounts.QueryElementAt(i, Components.interfaces.nsIMsgAccount);<br>  Application.console.log(account.key);<br>  // account.incomingServer is an nsIMsgIncomingServer<br>  // account.identities is an nsISupportsArray of nsIMsgIdentity objects<br>  //                    you can loop through it just like acctMgr.accounts above<br>  // account.defaultIdentity is an nsIMsgIdentity<br>}</code>
</pre>
<h2 id="Iterate_over_the_folders_in_an_account">Iterate over the folders in an account</h2>
<pre class="brush: js"><code>var acctMgr = Components.classes["@mozilla.org/messenger/account-manager;1"]<br>                        .getService(Components.interfaces.nsIMsgAccountManager);<br>var accounts = acctMgr.accounts;<br>for (var i = 0; i &lt; accounts.Count(); i++) {<br>  var account = accounts.QueryElementAt(i, Components.interfaces.nsIMsgAccount);<br>  var rootFolder = account.incomingServer.rootFolder; // nsIMsgFolder<br>  Application.console.log(rootFolder.prettiestName);<br>  if (rootFolder.hasSubFolders) {<br>    var subFolders = rootFolder.subFolders; // nsIMsgFolder<br>    while(subFolders.hasMoreElements()) {<br>      var folder = subFolders.getNext().QueryInterface(Components.interfaces.nsIMsgFolder);<br></code><code>      Application.console.log(</code><code>folder.prettiestName);<br>    }<br>  }<br>}</code></pre>
Revert to this revision