Board index » delphi » Getting user email address & mail server from Registry

Getting user email address & mail server from Registry

Since I will not know the user's email server and address in advance, I
need to get this info from their Registry so I can use Winsock's SMTP
for them to send mail.  I am able to do this if they use Netscape (I put
the code at the bottom of the message in case anybody is interested) but
can't do it for Outlook Express as I don't have it installed and
therefore there is no entries for this in my registry.

So, if anyone can send me the Registry keys for email server and email
address for Microosft's Outlook and Outlook Express I will greatly
appreciate it.  Here's how I do it for the Netscape I have installed on
my machine (I based this on the code at Delphi-Jedi):
======================================================
// MyServer and MyMail are global string variables
var SBaseKey,MBaseKey,STheKey,MTheKey,workS,workM : string;
begin
SBaseKey:='Software\Netscape\Netscape Navigator\Services';
MBaseKey:='Software\Netscape\Netscape Navigator\User';
STheKey:='SMTP_Server';
MTheKey:='User_Addr';
workS:='';
workM:='';
Reg := TRegistry.Create;

try
Reg.RootKey := HKEY_CURRENT_USER ;
if Reg.OpenKey(SBaseKey, False) then begin
   if Reg.ValueExists(STheKey) then begin
        workS:=Reg.ReadString(STheKey);
        Reg.CloseKey;
      end;
Reg.RootKey := HKEY_CURRENT_USER ;
   if Reg.OpenKey(MBaseKey, False) then
     if Reg.ValueExists(MTheKey) then
        workM:=Reg.ReadString(MTheKey);

if ((pos('.',workS) > 1) AND (pos('@',workM) > 1)) then begin
   MyServer:=workS;
   MyMail:=  workM;
   WriteMail;
   result:=true;
   exit;
end; {if both server and user mail address have valid entries }
finally
// more code here
end;
end;

 

Re:Getting user email address & mail server from Registry


See the unit below (BaclMsOutlookHelper.pas).  It will detect/retreive
settings from various flavors of Outlook (2000, '98, Express, etc.) and
released under MPL 1.1.  Also part of my code lib "BACL", I have units for
Netscape flavors, Opera, Eudora, etc.  If you need them, please feel free to
contact me about them.

Sorry if this posted twice - I was having some technical difficulty :)

Hope that helps,

Bryan Ashby
bas...@NOSPAM.FOR.ME.THANK.YOU.iaccess.com

--[snip]--
{***************************************************************************
***}
{
   }
{ Bryan Ashby's Code Library
       }
{ MS Outlook
       }
{
   }
{ Author...............Bryan Ashby
                }
{ Original
pas                                 }
{
                            }
{ Last
                         }
{
   }
{ The contents of this file are subject to the Mozilla
       }
{ License Version 1.1 (the "License"); you may not use this
     }
{ except in compliance with the License. You may obtain a copy
   }
{ the License at
                            }
{
   }
{ Software distributed under the License is distributed on an
    }
{ IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
   }
{ implied. See the License for the specific language
          }
{ rights and limitations under the
         }
{
 }
{ The Original Code is Bryan Ashby's Code Library
        }
{
   }
{ The Initial Developer of the Original Code is Bryan Ashby.  Portions
created }
{ by Bryan Ashby are Copyright (C) 2000, 2001 Bryan Ashby.  All
       }
{
          }
{
   }
{
                }
{
   }
{ Revision
         }
{       2001-02-09:  Now using OUTLOOK_SMTP_DISPLAY_NAME instead
   }
{       OUTLOOK_ACCOUNT_NAME when detecting installed Outlook version -
is   }
{       should be much more
          }
{       2001-02-14:  Added GetMsOutlookDefaultAccount
          }
{       2001-Mar-30:  Renamed GetMsOutlookDefaultAccount
   }
{       GetMsOutlookDefaultAccountStr which returns a nnnnnnnn string
ue    }
{       and added GetMsOutlookDefaultAccountId which returns a DWORD
ud.    }
{       The following functions now use RegOpenKeyEx instead
   }
{       RegCreateKeyEx: GetMsOutlookDefaultAccountStr,
GetDetectedOutlookType, }
{       and
                              }
{       Removed end backslash \ from
XPRESS.                  }
{       Now properly setting DataSize before each call to RegQueryValueEx
   }
{
                              }
{       2001-Apr-06: Removed GetMsOutlookDefaultAccountId function
      }
{       discovering that some versions of Outlook do not store account IDs
in  }
{       a numerical format.  GetMsOutlookAccountEntryInfo now expects
   }
{       AccountId to be a string, ie 00000001 or
          }
{       2001-Apr-09: Fixed bug in GetMsOutlookDefaultAccountStr that
    }
{       causing account 00000001 to always be detected as the default
account. }
{
   }
{***************************************************************************
***}

unit BaclMsOutlookHelper;

interface

uses
  Windows,

  SysUtils;

const
  //
  // Ms Outlook Account Registry Bases
  //
  OUTLOOK_ACCOUNT_2000 =
    'Software\Microsoft\Office\Outlook\OMI Account Manager\Accounts';
  OUTLOOK_ACCOUNT_98 =
    'Software\Microsoft\Office\8.0\Outlook\OMI Account Manager\Accounts';
  OUTLOOK_ACCOUNT_EXPRESS =
    'Software\Microsoft\Internet Account Manager\Accounts';

  //
  // Ms Outlook Account Entry Registry Names
  //
  OUTLOOK_ACCOUNT_NAME          = 'Account Name';
  OUTLOOK_POP3_SERVER           = 'POP3 Server';
  OUTLOOK_POP3_USER_NAME        = 'POP3 User Name';
  OUTLOOK_SMTP_DISPLAY_NAME     = 'SMTP Display Name';
  OUTLOOK_SMTP_EMAIL_ADDRESS    = 'SMTP Email Address';
  OUTLOOK_SMTP_SERVER           = 'SMTP Server';

type
  //
  // Supported Outlook Installation Types
  //
  TOutlookTypes = (otNone, ot98, ot2000, otExpress);

function GetMsOutlookDefaultAccountStr (const OutlookType : TOutlookTypes) :
  string;
{function GetMsOutlookDefaultAccountId (const OutlookType : TOutlookTypes) :
  DWORD; }
{function GetMsOutlookAccountEntryInfo (const OutlookType : TOutlookTypes;
  const AccountId : DWORD;  const EntryId : string) : string; }
function GetMsOutlookAccountEntryInfo (const OutlookType : TOutlookTypes;
  const AccountId : string;  const EntryId : string) : string;
function GetDetectedOutlookType : TOutlookTypes;

implementation

//--------------------------------------------------------------------------
----

function GetMsOutlookDefaultAccountStr (const OutlookType : TOutlookTypes) :
  string;
const
  OUTLOOK_ACCOUNT_DEF_MAIL = 'Default Mail Account';

  //
  // These are simular to the global constants found above, except
  // for the trailing \Accounts part - Account Manager is as far as we
  // want to go here.
  //
  OUTLOOK_ACCOUNT_2000 =
    'Software\Microsoft\Office\Outlook\OMI Account Manager';
  OUTLOOK_ACCOUNT_98 =
    'Software\Microsoft\Office\8.0\Outlook\OMI Account Manager';
  OUTLOOK_ACCOUNT_EXPRESS =
    'Software\Microsoft\Internet Account Manager';

var
  OpenKey       : HKEY;
  SubKey        : string;
  DataType,
  DataSize      : DWORD;
  Temp          : array [0..2048] of char;
begin
  //
  // Default to the first account
  //
  Result := '00000001';

  case OutlookType of
    otNone :    Exit;
    ot98 :      SubKey := OUTLOOK_ACCOUNT_98;
    ot2000 :    SubKey := OUTLOOK_ACCOUNT_2000;
    otExpress : SubKey := OUTLOOK_ACCOUNT_EXPRESS;
  end;

  if RegOpenKeyEx (HKEY_CURRENT_USER, PChar(SubKey),
REG_OPTION_NON_VOLATILE,
    KEY_READ, OpenKey) = ERROR_SUCCESS then
  begin
    DataType := REG_SZ;
    DataSize := SizeOf(Temp);
    if RegQueryValueEx (OpenKey, OUTLOOK_ACCOUNT_DEF_MAIL, nil, @DataType,
      @Temp, @DataSize) = ERROR_SUCCESS then
      Result := string(Temp);
    RegCloseKey (OpenKey);
  end;
end;

//--------------------------------------------------------------------------
----
(*
function GetMsOutlookDefaultAccountId (const OutlookType : TOutlookTypes) :
  DWORD;
const
  OUTLOOK_ACCOUNT_DEF_MAIL = 'Default Mail Account';
var
  OpenKey       : HKEY;
  SubKey        : string;
  DataType,
  DataSize      : DWORD;
  Buffer        : array [0..2048] of char;
  Temp          : string;
begin
  //
  // Default to first account
  //
  Result := 1;

  case OutlookType of
    otNone :    Exit;
    ot98 :      SubKey := OUTLOOK_ACCOUNT_98;
    ot2000 :    SubKey := OUTLOOK_ACCOUNT_2000;
    otExpress : SubKey := OUTLOOK_ACCOUNT_EXPRESS;
  end;

  if RegOpenKeyEx (HKEY_CURRENT_USER, PChar(SubKey),
REG_OPTION_NON_VOLATILE,
    KEY_READ, OpenKey) = ERROR_SUCCESS then
  begin
    DataType := REG_SZ;
    DataSize := SizeOf(Buffer);
    if RegQueryValueEx (OpenKey, OUTLOOK_ACCOUNT_DEF_MAIL, nil, @DataType,
      @Buffer, @DataSize) = ERROR_SUCCESS then
    begin
      Temp := string(Buffer);
      Result := StrToIntDef (Temp,1);
    end;
    RegCloseKey (OpenKey);
  end;
end;
*)
//--------------------------------------------------------------------------
----
(*
function GetMsOutlookAccountEntryInfo (const OutlookType : TOutlookTypes;
  const AccountId : DWORD;  const EntryId : string) : string;
var
  OpenKey       : HKEY;
  Temp          : array [0..2048] of char;
  DataSize,
  DataType      : Integer;
  SubKey        : string;
begin
  //
  // Check for errornous AccountId
  //
  if AccountId <= 0 then
    Exit;

  //
  // Determin Sub Key to open
  //
  case OutlookType of
    otNone : Exit;
    ot98 : SubKey := OUTLOOK_ACCOUNT_98;
    ot2000 : SubKey := OUTLOOK_ACCOUNT_2000;
    otExpress : SubKey := OUTLOOK_ACCOUNT_EXPRESS;
  end;
  SubKey := SubKey + '\' + Format ('%.8d', [AccountId]);

  if RegOpenKeyEx (HKEY_CURRENT_USER, PChar(SubKey),
REG_OPTION_NON_VOLATILE,
    KEY_READ, OpenKey) = ERROR_SUCCESS then
  begin
    DataType := REG_SZ;
    DataSize := SizeOf(Temp);
    if RegQueryValueEx (OpenKey, PChar(EntryId), nil, @DataType, @Temp,
      @DataSize) = ERROR_SUCCESS then
      Result := string(Temp)
    else
      Result := '';
    RegCloseKey (OpenKey);
  end;
end;
*)

//--------------------------------------------------------------------------
----

function GetMsOutlookAccountEntryInfo (const OutlookType : TOutlookTypes;
  const AccountId : string;  const EntryId : string) : string;
var
  OpenKey       : HKEY;
  Temp          : array [0..2048] of char;
  DataSize,
  DataType      : Integer;
  SubKey        : string;
begin
  //
  // Check for errornous AccountId
  //
  if Length(AccountId) < 8 then
    Exit;

  //
  // Determin Sub Key to open
  //
  case OutlookType of
    otNone : Exit;
    ot98 : SubKey := OUTLOOK_ACCOUNT_98;
    ot2000 : SubKey := OUTLOOK_ACCOUNT_2000;
    otExpress : SubKey := OUTLOOK_ACCOUNT_EXPRESS;
  end;

  SubKey := SubKey + '\' + AccountId;

  if RegOpenKeyEx (HKEY_CURRENT_USER, PChar(SubKey),
REG_OPTION_NON_VOLATILE,
    KEY_READ, OpenKey) = ERROR_SUCCESS then
  begin
    DataType := REG_SZ;
    DataSize := SizeOf(Temp);
    if RegQueryValueEx (OpenKey, PChar(EntryId), nil, @DataType, @Temp,
      @DataSize) = ERROR_SUCCESS then
      Result := string(Temp)
    else
      Result := '';
    RegCloseKey (OpenKey);
  end;
end;

//--------------------------------------------------------------------------
----

function GetDetectedOutlookType : TOutlookTypes;
const
  OUTLOOK_FIRST_ACCOUNT = '00000001';
var
  OpenKey       : HKEY;
  DataSize,
  DataType      : Integer;
  Temp          : array [0..2048] of char;
begin
  Result := otNone;

  DataType := REG_SZ;

  //
  // Outlook Express
  //
  if RegOpenKeyEx (HKEY_CURRENT_USER, OUTLOOK_ACCOUNT_EXPRESS + '\' +
    OUTLOOK_FIRST_ACCOUNT, REG_OPTION_NON_VOLATILE, KEY_READ, OpenKey) =
    ERROR_SUCCESS then
  begin
    DataSize := SizeOf(Temp);
    if RegQueryValueEx (OpenKey, OUTLOOK_SMTP_DISPLAY_NAME, nil, @DataType,
      @Temp, @DataSize) = ERROR_SUCCESS then
      if not (string(Temp) = '') then
        Result := otExpress;
    RegCloseKey (OpenKey);
  end;

  FillChar (Temp, SizeOf(Temp), #0);

  //
  // Outlook 98
  //
  if
...

read more »

Re:Getting user email address & mail server from Registry


Quote
Bryan Ashby wrote:

> See the unit below (BaclMsOutlookHelper.pas).  It will detect/retreive
> settings from various flavors of Outlook (2000, '98, Express, etc.) and
> released under MPL 1.1.  Also part of my code lib "BACL", I have units for
> Netscape flavors, Opera, Eudora, etc.  If you need them, please feel free to
> contact me about them.

This is what I am also looking for,
I would be very gratefull for the netscape, opera and eudora units.
Any change for a unit for lotus ?

--
Guido Geurts
System Engineer

www.ADTechno.com
Guido.Geu...@adtechno.be

Never knock on Death's door. Ring his doorbell and run.  (He hates
that.)

Re:Getting user email address & mail server from Registry


Sorry, no Lotus, but feel free to email me and I'll send you the units
rather than posting all of them here.

Bryan Ashby

Quote
"Guido Geurts" <Guido.Geu...@adtechno.be> wrote in message

news:3B57E4E7.7AA88C7E@adtechno.be...
Quote
> Bryan Ashby wrote:

> > See the unit below (BaclMsOutlookHelper.pas).  It will detect/retreive
> > settings from various flavors of Outlook (2000, '98, Express, etc.) and
> > released under MPL 1.1.  Also part of my code lib "BACL", I have units
for
> > Netscape flavors, Opera, Eudora, etc.  If you need them, please feel
free to
> > contact me about them.

> This is what I am also looking for,
> I would be very gratefull for the netscape, opera and eudora units.
> Any change for a unit for lotus ?

> --
> Guido Geurts
> System Engineer

> www.ADTechno.com
> Guido.Geu...@adtechno.be

> Never knock on Death's door. Ring his doorbell and run.  (He hates
> that.)

Re:Getting user email address & mail server from Registry


Why don't you read a little more carefully?  I said FOR THEM to send
mail, I am not going to make any use of it myself.  I simply want to
offer a default for the sake of those who would get scared if asked for
something like "mail server" without a default value.
Quote
Chipper wrote:

> On Thu, 19 Jul 2001 11:27:27 -0700, alley...@scn.org wrote:

> >Since I will not know the user's email server and address in advance, I
> >need to get this info from their Registry so I can use Winsock's SMTP
> >for them to send mail.

> Did you try "Asking" the user?     Thank god I got a firewall to
> protect myself from {*word*67}s like you.

Other Threads