如何用Java程序获取Windows登录用户的邮箱地址?
在企业Windows环境下用Java获取登录用户邮箱的可行方案
一、调用Windows系统命令(零依赖)
企业AD环境中,用户的用户主体名称(UPN)通常与邮箱地址一致,可以通过执行Windows原生命令whoami /upn直接获取。Java通过执行系统进程读取输出即可:
import java.io.BufferedReader; import java.io.InputStreamReader; public class WindowsUserEmailFetcher { public static void main(String[] args) { try { Process process = Runtime.getRuntime().exec("whoami /upn"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String userEmail = reader.readLine().trim(); reader.close(); process.waitFor(); System.out.println("当前用户邮箱: " + userEmail); } catch (Exception e) { e.printStackTrace(); } } }
注意:该方案的前提是企业AD配置中用户UPN与邮箱地址一致,这是绝大多数企业域环境的标准配置。
二、LDAP查询Active Directory(精准获取)
如果UPN与邮箱不一致,或者需要直接读取AD中存储的邮箱属性,可通过Java的JNDI连接企业AD进行查询:
import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.directory.Attributes; import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; import javax.naming.ldap.InitialLdapContext; import java.util.Hashtable; public class ADUserEmailQuery { public static void main(String[] args) { Hashtable<String, String> env = new Hashtable<>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // 替换为企业实际AD服务器地址和域根DN env.put(Context.PROVIDER_URL, "ldap://your-ad-server:389/DC=your-company,DC=com"); // 使用当前登录的域用户身份自动绑定 env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI"); try { InitialLdapContext ctx = new InitialLdapContext(env, null); SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); // 获取当前登录的用户名 String currentUsername = System.getProperty("user.name"); // 查询AD中该用户的mail属性 NamingEnumeration<SearchResult> results = ctx.search( "", "(sAMAccountName=" + currentUsername + ")", controls ); if (results.hasMoreElements()) { SearchResult result = results.nextElement(); Attributes attrs = result.getAttributes(); String email = attrs.get("mail").get().toString(); System.out.println("当前用户邮箱: " + email); } else { System.out.println("未在AD中找到该用户的邮箱信息"); } ctx.close(); } catch (Exception e) { e.printStackTrace(); } } }
说明:
- 确保程序运行的用户拥有AD只读权限(企业域用户默认具备)
- 代码中的AD服务器地址、域根DN需要替换为企业实际配置
mail是AD存储用户邮箱的标准属性,若企业使用其他属性可自行替换
三、第三方库简化LDAP操作(Apache Directory LDAP API)
如果觉得JNDI使用繁琐,可借助Apache Directory LDAP API简化AD查询,需先引入Maven依赖:
<dependency> <groupId>org.apache.directory.api</groupId> <artifactId>api-all</artifactId> <version>2.1.5</version> </dependency>
示例代码:
import org.apache.directory.api.ldap.model.entry.Entry; import org.apache.directory.api.ldap.model.message.SearchScope; import org.apache.directory.ldap.client.api.LdapConnection; import org.apache.directory.ldap.client.api.LdapNetworkConnection; public class ApacheLDAPEmailFetcher { public static void main(String[] args) { String adHost = "your-ad-server"; int adPort = 389; String baseDn = "DC=your-company,DC=com"; String currentUsername = System.getProperty("user.name"); try (LdapConnection connection = new LdapNetworkConnection(adHost, adPort)) { // 使用当前Windows域用户身份绑定 connection.bindGssApi(); // 查询用户邮箱属性 Entry userEntry = connection.lookup( baseDn, "(sAMAccountName=" + currentUsername + ")", SearchScope.SUBTREE, "mail" ); String email = userEntry.get("mail").getString(); System.out.println("当前用户邮箱: " + email); } catch (Exception e) { e.printStackTrace(); } } }
内容的提问来源于stack exchange,提问作者Praveen Balasubramaniam
相关产品推荐
相关产品推荐

