Click here to Skip to main content
15,310,682 members
Articles / Desktop Programming / Win32
Article
Posted 19 Aug 2018

Stats

16.5K views
384 downloads
4 bookmarked

How to Find User's Email Address

Rate me:
Please Sign up or sign in to vote.
4.03/5 (14 votes)
19 Aug 2018CPOL1 min read
Here is a way to find what is the email address / addresses used by the user running a program

Introduction

Following a question in Stack Overflow, I wrote this article which is about finding the user's email address, or several email addresses if he/she has.

Background

I looked at the Registry to find out how to do so, by searching for my own email address at the entire Registry. This is rather a tricky method to find out (in all probability) the user's email address from the stored email used to log on to Microsoft services. There are many email clients and methods, among them web mail (Gmail, Yahoo, etc.) and desktop clients (Outlook) and yet, assuming the typical user will use the main email address to sign in to Windows (and most of the Windows users use their Windows account's credentials to even log in to their PC), this works!

Using the Code

Windows stores used email accounts used as "Live ID" in the "UserExtendedProperties" key in:

HKEY_CURRENT_USER\Software\Microsoft\IdentityCRL

The assumption is that this email was selected by the user as the Windows user name to log in to the PC and/or to other Windows services.

Therefore, you can get the email accounts using the following code:

C++
//
// void GetDefaultEmailAddress()
{
    HKEY key;
    TCHAR    achKey[MAX_KEY_LENGTH];        // buffer for subkey name
    DWORD    cbName;                        // size of name string 
    TCHAR    achClass[MAX_PATH] = TEXT(""); // buffer for class name 
    DWORD    cchClassName = MAX_PATH;       // size of class string 
    DWORD    cSubKeys = 0;                  // number of subkeys 
    DWORD    cbMaxSubKey;                   // longest subkey size 
    DWORD    cchMaxClass;                   // longest class string 
    DWORD    cValues;                       // number of values for key 
    DWORD    cchMaxValue;                   // longest value name 
    DWORD    cbMaxValueData;                // longest value data 
    DWORD    cbSecurityDescriptor;          // size of security descriptor 
    FILETIME ftLastWriteTime;               // last write time 

    DWORD i, retCode;

    TCHAR  achValue[MAX_VALUE_NAME];
    DWORD cchValue = MAX_VALUE_NAME;

    if (RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\IdentityCRL\\UserExtendedProperties", 
                     NULL, KEY_READ, &key) == ERROR_SUCCESS)
    {
        // Get the class name and the value count. 
        retCode = RegQueryInfoKey(
            key,                            // key handle 
            achClass,                       // buffer for class name 
            &cchClassName,                  // size of class string 
            NULL,                           // reserved 
            &cSubKeys,                      // number of subkeys 
            &cbMaxSubKey,                   // longest subkey size 
            &cchMaxClass,                   // longest class string 
            &cValues,                       // number of values for this key 
            &cchMaxValue,                   // longest value name 
            &cbMaxValueData,                // longest value data 
            &cbSecurityDescriptor,          // security descriptor 
            &ftLastWriteTime);              // last write time 

        // Enumerate the email accounts subkeys, until RegEnumKeyEx fails.

        if (cSubKeys)
        {
            wprintf(TEXT("\nNumber of email accounts used: %d\n"), cSubKeys);

            for (i = 0; i < cSubKeys; i++)
            {
                cbName = MAX_KEY_LENGTH;
                retCode = RegEnumKeyEx(key, i,
                    achKey,
                    &cbName,
                    NULL,
                    NULL,
                    NULL,
                    &ftLastWriteTime);
                if (retCode == ERROR_SUCCESS)
                {
                    wprintf(TEXT("(%d) %s\n"), i + 1, achKey);
                }
            }
        }
    }
}

//

Points of Interest

When it comes to desktop applications used for email (i.e., MAPI clients), the place to look in order to enumerate these clients, is the:

Software\Clients\Mail Key in HKEY_LOCAL_MACHINE.

There, you will find all the installed MAPI clients.

You can also determine the default one by looking at:

HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Mail\Default.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Michael Haephrati
CEO Secured Globe, Inc.
United States United States
Michael Haephrati, Musician and CEO and co-founder of Secured Globe, Inc. Worked on many ventures starting from HarmonySoft, designing Rashumon, the first Graphical Multi-lingual word processor for Amiga computer. During 1995-1996 he worked as a Contractor with Apple at Cupertino.

You can also Buy me coffee.





Comments and Discussions

 
QuestionAll Right Then Pin
Rick York19-Aug-18 15:58
mveRick York19-Aug-18 15:58 
AnswerRe: All Right Then Pin
Michael Haephrati19-Aug-18 23:14
mvaMichael Haephrati19-Aug-18 23:14 
QuestionMy vote of 5 Pin
James Rok19-Aug-18 8:09
MemberJames Rok19-Aug-18 8:09 
SuggestionI can see why it works Pin
John Klinner19-Aug-18 7:47
MemberJohn Klinner19-Aug-18 7:47 
Great article. I checked it and it showed my own (real) email and I can see that since people use their real and active email to sign in to Windows, this method makes sense. My email client is Outlook so this method would probably work most of the times regardless of your email client method (web, desktop) or your provider.
QuestionMicrosoft Live sign only? Pin
Wendelius19-Aug-18 6:26
mveWendelius19-Aug-18 6:26 
AnswerRe: Microsoft Live sign only? Pin
Jim Forst19-Aug-18 6:51
MemberJim Forst19-Aug-18 6:51 
AnswerRe: Microsoft Live sign only? Pin
Michael Haephrati19-Aug-18 7:19
mvaMichael Haephrati19-Aug-18 7:19 
GeneralRe: Microsoft Live sign only? Pin
Ralph Walden20-Aug-18 6:49
MemberRalph Walden20-Aug-18 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.