Reputation: 3689
Is there a way to get the user's email address from within Windows via Win32 or .NET? Is there a registry key or API that contains this information?
EDIT: I have an application that emails my company if our application fails and I wanted to get a return email address so that we could respond that individual that experienced the crash. I'm currently getting the username, but that may not match the email name. Obviously I can get the user to enter his email address, but the interface would be a little friendlier if I could at least attempt to acquire the email address and have the user verify that the return email address is correct.
Upvotes: 17
Views: 5981
Reputation: 3372
I know this is an old question but if like me you arrive here, as per this answer on Superuser
https://superuser.com/questions/836220/get-email-address-of-current-logged-in-user
On CMD run whoami /upn
It gives the user principal which is often the default email
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/whoami
Upvotes: 0
Reputation: 4235
Windows stores used email accounts in the "UserExtendedProperties
" key in
HKEY_CURRENT_USER\Software\Microsoft\IdentityCRL
So you can get the email accounts using the following code:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
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);
}
}
}
}
}
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.
You will find there all installed [MAPI clients][1].
.
You can also determine the default one by looking at:
HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Mail\Default.
See also: article and tool / source code to download
Upvotes: 2
Reputation: 347416
You could try using the NameUserPrincipal
constant from the EXTENDED_NAME_FORMAT
enumeration with the GetUserNameEx
function.
NameUserPrincipal The user principal name (for example, [email protected]).
But I would only recommend using this as a pre-filled address in a prompt to the user.
There is more than a good chance it will fail with GetLastError
of ERROR_NONE_MAPPED
though if the info is not available.
Upvotes: 2
Reputation: 11638
The only way I can think that this would make sense is in a Windows Active Directory environment. In this case you can query AD and see if there's an email address associated with the user's account. This will definitely work with MS Exchange and may also work with other enterprise email systems. For .Net you can use the classes in the System.DirectoryServices namespace. For Win32 you can use the ADSI API. You will have to read up on AD and create a suitable query to match your requirements.
Upvotes: 5
Reputation: 693
I think the simple answer is no ... but of course the email address will be stored in their email program such as Outlook.
What is it you are trying to achieve?
Upvotes: 1
Reputation: 3009
There may be SOME email address stored within Windows, but for you to get a user's actual email address, you have to have them type it in, and to assure it, you have to handshake by sending them an activate email before you use it.
Upvotes: 0
Reputation: 16505
Email addresses could be for web-based clients like gmail or they could be domain email addresses. Either way the implementation would have to be based on the specifics of the user's email setup. So the short answer is "no", at least there's no "one-size-fits-all" method.
Upvotes: 0
Reputation: 135111
Let me answer you by asking you this: Did you ever enter your email address when installing windows?
Upvotes: 3
Reputation: 9922
Have you saved your e-mail address somewhere in the system? There is no standard place to look for. I always depends on the applications the user uses (Outlook, Outlook Express, TuhunderBird).
The best way to get the users e-mail address is to ask him.
Upvotes: 3