Retrieve E-mail accounts from Thunderbird (email client)

Hi,

How can I retrieve the e-mail accounts (pop3) from thunderbird?
Currently I use the code below to retrieve these from Outlook Express.
Scanning through the registry it seems Thunderbird doesn't use the registry to store the accounts???


procedure TForm1.HaalEmailAccountsOp(MijnCombo : TComboBox);
var
  Registry   : TRegistry;
  MijnLijst  : Tstrings;
  i          : Integer;
begin
  MijnLijst        := TStringList.Create;
  Registry         := Tregistry.Create;
  Registry.RootKey := HKEY_CURRENT_USER;
  Registry.OpenKey('\Software\Microsoft\Internet Account Manager\Accounts',False);
  Registry.GetKeyNames(MijnLijst);
  Registry.CloseKey;
  For i := 0 to MijnLijst.Count-1 do
  begin
    Registry.OpenKey('\Software\Microsoft\Internet Account Manager\Accounts\'+MijnLijst[i],False);
    If Registry.ValueExists('SMTP Email Address') then
    begin
      MijnCombo.Items.Add(Registry.ReadString('SMTP Email Address'));
    end;
    Registry.CloseKey;
  end;
  MijnLijst.Free;
  Registry.Free;
end;


Please supply some working code.
Thanks a lot for your help.
Stef
Stef MerlijnDeveloperAsked:
Who is Participating?
 
TheRealLokiSenior DeveloperCommented:
Here's a real quick example I wrote for you. Never looked at Thunderbird settings before but thought I'd jump right into it

put a button and a memo on a form

!! Don't forget to hook up the FormCreate, FormDestroy, and ButtonClick events! !
THis should work if you have hunderbird installed on teh same machine with the current logged on user
if you are not runnin g it this way then set
ProfileFilename manually

hth, Loki

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Registry, IniFiles;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
        PrefsJS: TStringList;
        ProfileFilename: string;
  public
    { Public declarations }
    procedure LoadPrefsJS;
    function NumberOfPOP3Accounts: integer;
    procedure DisplayPOP3AccountData;
  end;

var
  Form1: TForm1;

IMPLEMENTATION

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
    begin
        PrefsJS := TStringList.Create;
    end;

procedure TForm1.FormDestroy(Sender: TObject);
    begin
        PrefsJS.Clear;
        PrefsJS.Free;
    end;

procedure TForm1.Button1Click(Sender: TObject);
//const
//    RootApplicationDir
    var
        reg : TRegistry;
        i : cardinal;
        sAppData: string;
        sUser: string;
    begin
// first we try to get teh Application Data folder
        reg := TRegistry.Create;
        try
            reg.RootKey := HKEY_CURRENT_USER;
            reg.LazyWrite := false;
            reg.OpenKey(
            'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',
            false);
            sAppData := reg.ReadString('AppData');
            memo1.lines.add(sAppData);
            reg.CloseKey
        finally
            reg.free;
        end;

// next we read the default profile path from profile.ini
        with TIniFile.Create(sAppData + '\Thunderbird\Profiles.ini') do
        try
            ProfileFilename :=
            sAppData + '\Thunderbird\' +
            StringReplace(ReadString('Profile0', 'Path', ''), '/', '\', [rfReplaceAll]) +
            '\prefs.js';
        finally
            free;
        end;

        memo1.lines.add(ProfileFilename);

        LoadPrefsJS;
        Memo1.Lines.Add('****************************');
        Memo1.Lines.Add('POP3 Data');
        Memo1.Lines.Add('****************************');
        Memo1.Lines.Add('There are ' + IntToStr(NumberOfPOP3Accounts) + ' POP3 accounts');
        DisplayPOP3AccountData;

        Memo1.Lines.Add('****************************');
        Memo1.Lines.Add('ALL THE STORED VALUES');
        Memo1.Lines.Add('****************************');
        memo1.lines.addstrings(prefsJS);

    end;

procedure TForm1.LoadPrefsJS;
    var
        i, j: integer;
        S, SName, Svalue: string;
        tempSL: TStringList;
    begin
        tempSL := TStringList.Create;
        try
            tempSL.LoadFromFile(ProfileFilename);
// while loop to reformat. Strip off the 'user_pref("' and use "values/names" of TStringList
            for i := 0 to pred(tempSL.Count) do
            begin
                s := tempSL[i];
                if pos('user_pref("', lowercase(s)) = 1 then
                begin
                    delete(s, 1, length('user_pref("'));
                    j := pos('", ', S);
                    SName := copy(s, 1, j-1);
                    delete(S, 1, (j-1) + length('", '));
                    if s[1] = '"'then delete(S, 1, 1);
                    j := pos('"', S);
                    SValue := copy(S, 1, j-1);
                    PrefsJS.Values[SName] := SValue; // load the value into stringlist
                end;
            end;
        finally
            tempSL.Clear;
            tempSL.Free;
        end;
    end;

function TForm1.NumberOfPOP3Accounts: integer;
    var
        i: integer;
    begin
        result := 0;
        i := 1;
        while PrefsJS.Values['mail.server.server' + IntToStr(i) + '.type'] <> '' do
        begin
            if lowercase(PrefsJS.Values['mail.server.server' + IntToStr(i) + '.type']) = 'pop3' then
              result := result + 1;
            inc(i);
        end;
    end;

procedure TForm1.DisplayPOP3AccountData;
    var
        i: integer;
    begin
        i := 1;
        while PrefsJS.Values['mail.server.server' + IntToStr(i) + '.type'] <> '' do
        begin
            if lowercase(PrefsJS.Values['mail.server.server' + IntToStr(i) + '.type']) = 'pop3' then
            begin
//it's a pop3 account, so lets get the information
// display the username
                memo1.Lines.Add('Account ' + IntToStr(i) + ' Username = ' +
                  PrefsJS.Values['mail.server.server' + IntToStr(i) + '.username']);
// display the pop3 hostname
                memo1.Lines.Add('Account ' + IntToStr(i) + ' pop3 hostname = ' +
                  PrefsJS.Values['mail.server.server' + IntToStr(i) + '.hostname']);
            end;
            inc(i);
        end;

// display smtp servers
        i := 1;
        while PrefsJS.Values['mail.smtpserver.smtp' + IntToStr(i) + '.hostname'] <> '' do
        begin
             memo1.Lines.Add('SMTP Server (not account specific) ' + IntToStr(i) + ' hostname = ' +
               PrefsJS.Values['mail.smtpserver.smtp' + IntToStr(i) + '.hostname']);
            inc(i);
        end;
    end;

end.
0
 
robert_marquardtCommented:
All Mozilla family browsers use a prefs.js file.which is stored in the preferences directory of the user.
\Documents and Settings\<User>\Application Data\Mozilla\Profiles\default\<some generated name.slt\prefs.js

Do not forget that the name "\Documents and Settings\<User>\Application Data" is localized.
0
 
Stef MerlijnDeveloperAuthor Commented:
Robert, thank you for that.
But how can I retrieve the email-account from there from within Delphi (like above for Outlook Express)?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
robert_marquardtCommented:
This is a text file (in fact a progam source to be interpreted). Have a look at it. It should be easy to understand.

Here an excerpt from my file (with deleted names)

user_pref("mail.account.account1.identities", "id1");
user_pref("mail.account.account1.server", "server1");
user_pref("mail.account.account2.server", "server2");
user_pref("mail.account.account3.identities", "id2");
user_pref("mail.account.account3.server", "server3");
user_pref("mail.account.account4.identities", "id3");
user_pref("mail.account.account4.server", "server4");
user_pref("mail.accountmanager.accounts", "account2,account3,account1,account4");
user_pref("mail.accountmanager.defaultaccount", "account2");
user_pref("mail.accountmanager.localfoldersserver", "server2");
user_pref("mail.compose.attach.dir", "<a path>");
user_pref("mail.default_html_action", 1);
user_pref("mail.display_glyph", false);
user_pref("mail.forward_message_mode", 2);
user_pref("mail.identity.id1.compose_html", false);
user_pref("mail.identity.id1.doBcc", false);
user_pref("mail.identity.id1.draft_folder", "mailbox://nobody@Local%20Folders/Drafts");
user_pref("mail.identity.id1.drafts_folder_picker_mode", "0");
user_pref("mail.identity.id1.escapedVCard", "");
user_pref("mail.identity.id1.fcc_folder", "mailbox://nobody@Local%20Folders/Sent");
user_pref("mail.identity.id1.fcc_folder_picker_mode", "0");
user_pref("mail.identity.id1.fullName", "xxxxxx");
user_pref("mail.identity.id1.organization", "");
user_pref("mail.identity.id1.reply_to", "");
user_pref("mail.identity.id1.smtpServer", "smtp1");
user_pref("mail.identity.id1.stationery_folder", "mailbox://nobody@Local%20Folders/Templates");
user_pref("mail.identity.id1.tmpl_folder_picker_mode", "0");
user_pref("mail.identity.id1.useremail", "xxxxx@yyyy.zz");
user_pref("mail.identity.id1.valid", true);
user_pref("mail.identity.id2.compose_html", false);
user_pref("mail.identity.id2.doBcc", false);
user_pref("mail.identity.id2.draft_folder", "mailbox://nobody@Local%20Folders/Drafts");
user_pref("mail.identity.id2.drafts_folder_picker_mode", "0");
user_pref("mail.identity.id2.escapedVCard", "");
user_pref("mail.identity.id2.fcc_folder", "mailbox://nobody@Local%20Folders/Sent");
user_pref("mail.identity.id2.fcc_folder_picker_mode", "0");
user_pref("mail.identity.id2.fullName", "xxxxxxx");
user_pref("mail.identity.id2.organization", "");
user_pref("mail.identity.id2.reply_to", "");
0
 
Stef MerlijnDeveloperAuthor Commented:
I realy need a complete solution to my problem.
Based on what you wrote I've no idea where to start.
0
 
Stef MerlijnDeveloperAuthor Commented:
Thanks you very much.  TheRealLoki
This is what I needed. I've stripped the code so only the useremails will be added into the memo.

unit ThunderbirdUnit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Registry, IniFiles;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
        PrefsJS: TStringList;
        ProfileFilename: string;
  public
    { Public declarations }
    procedure LoadPrefsJS;
    function NumberOfPOP3Accounts: integer;
    procedure DisplayPOP3AccountData;
  end;

var
  Form1: TForm1;

IMPLEMENTATION

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  PrefsJS := TStringList.Create;
  memo1.Clear;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  PrefsJS.Clear;
  PrefsJS.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var reg : TRegistry;
    i : cardinal;
    sAppData: string;
    sUser: string;
begin
  // first we try to get teh Application Data folder
  reg := TRegistry.Create;
  try
    reg.RootKey := HKEY_CURRENT_USER;
    reg.LazyWrite := false;
    reg.OpenKey(
    'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',
    false);
    sAppData := reg.ReadString('AppData');
    // memo1.lines.add(sAppData);
    reg.CloseKey
  finally
    reg.free;
  end;

  // next we read the default profile path from profile.ini
  with TIniFile.Create(sAppData + '\Thunderbird\Profiles.ini') do
  try
    ProfileFilename :=
    sAppData + '\Thunderbird\' +
    StringReplace(ReadString('Profile0', 'Path', ''), '/', '\', [rfReplaceAll]) +
    '\prefs.js';
  finally
    free;
  end;

  // Memo1.lines.add(ProfileFilename);
  LoadPrefsJS;
  // Memo1.Lines.Add('****************************');
  // Memo1.Lines.Add('POP3 Data');
  // Memo1.Lines.Add('****************************');
  // Memo1.Lines.Add('There are ' + IntToStr(NumberOfPOP3Accounts) + ' POP3 accounts');
  DisplayPOP3AccountData;
  // Memo1.Lines.Add('****************************');
  // Memo1.Lines.Add('ALL THE STORED VALUES');
  // Memo1.Lines.Add('****************************');
  // Memo1.lines.addstrings(prefsJS);
end;

procedure TForm1.LoadPrefsJS;
var i, j: integer;
    S, SName, Svalue: string;
    tempSL: TStringList;
begin
  tempSL := TStringList.Create;
  try
    tempSL.LoadFromFile(ProfileFilename);
    // while loop to reformat. Strip off the 'user_pref("' and use "values/names" of TStringList
    for i := 0 to pred(tempSL.Count) do
    begin
      s := tempSL[i];
      if pos('user_pref("', lowercase(s)) = 1 then
      begin
        delete(s, 1, length('user_pref("'));
        j := pos('", ', S);
        SName := copy(s, 1, j-1);
        delete(S, 1, (j-1) + length('", '));
        if s[1] = '"'then delete(S, 1, 1);
        j := pos('"', S);
        SValue := copy(S, 1, j-1);
        PrefsJS.Values[SName] := SValue; // load the value into stringlist
      end;
    end;
  finally
    tempSL.Clear;
    tempSL.Free;
  end;
end;

function TForm1.NumberOfPOP3Accounts: integer;
var i: integer;
begin
  result := 0;
  i := 1;
  while PrefsJS.Values['mail.server.server' + IntToStr(i) + '.type'] <> '' do
  begin
    if lowercase(PrefsJS.Values['mail.server.server' + IntToStr(i) + '.type']) = 'pop3' then
      result := result + 1;
    inc(i);
  end;
end;

procedure TForm1.DisplayPOP3AccountData;
var i: integer;
begin
  i := 1;
  while PrefsJS.Values['mail.identity.id' + IntToStr(i) + '.useremail'] <> '' do
  begin
    memo1.Lines.Add(PrefsJS.Values['mail.identity.id' + IntToStr(i) + '.useremail']);
    inc(i);
  end;
end;

end.

Thanks, Stef
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.