Click here to Skip to main content
15,891,136 members
Articles / Web Development / ASP.NET
Article

Query Exchange server and retrive email address from NT username

Rate me:
Please Sign up or sign in to vote.
1.86/5 (3 votes)
13 Aug 2007 40.6K   23   3
This is a simple methog to query an exchange server and retrive a remote users email address on a corporate intranet
  • <a href="Query_Exchange_from_NET/EmailFromNT.zip">Download EmailFromNT.zip - 1.3 KB</a> 

Introduction

After searching many diffrent articles I finally was able to do what I wanted. Using the corporate intranet I wanted to be able to email FROM the user that is using clicking the submit button from the webpage without using outlook. Seems simple enough right?

I found the best thing to do is to use Directory Services. So in your ASP .NET application right click the root node in the Solution explorer and add a referance on the .NET tab to:

System.DirectoryServices

and then also include this in the using clause. Once that is done the rest is pretty easy:

C#
//
// I created a simple class to be available anywhere in the application:


using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.DirectoryServices;

using System.Collections;

public class NTtoEmail

{

public NTtoEmail()

{



}

public string GetEmail(string ntname)

{

DirectorySearcher objsearch = new DirectorySearcher(); 

string strrootdse = objsearch.SearchRoot.Path;

DirectoryEntry objdirentry = new DirectoryEntry(strrootdse);

objsearch.Filter = "(& (mailnickname="+ ntname.Trim() + ")(objectClass=user))";

objsearch.SearchScope = System.DirectoryServices.SearchScope.Subtree;

objsearch.PropertiesToLoad.Add("mail");

objsearch.PropertyNamesOnly = true;

SearchResultCollection colresults = objsearch.FindAll();

string arl = "";

foreach (SearchResult objresult in colresults)

{

arl = arl + objresult.GetDirectoryEntry().Properties["mail"].Value + ",";

}

if (arl.Length > 0)

arl = arl.Substring(0, arl.Length - 1);

objsearch.Dispose(); 

return arl;

}

}

This is pretty simple. Now you can simply call this from within the webpage like this:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public partial class test : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

NTtoEmail mlc = new NTtoEmail();

TextBox1.Text = mlc.GetEmail(Request.ServerVariables["REMOTE_USER"].Substring(3, Request.ServerVariables["REMOTE_USER"].Length - 3)); 

}

}

Points of Interest

The servervariables returns the domain name as well. our corporate domain is 2 letters so this well work but if you have multiple domains I would suggest to use the split function.

Feel free to check out my website as well:

http://www.dbaoasis.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Database Developer
United States United States
DB2 / MSSQL Database Administrator / PeopleSoft Administrator

Comments and Discussions

 
QuestionYour website??? Pin
Andreas Kroll21-Aug-07 3:28
Andreas Kroll21-Aug-07 3:28 
What's the point of directing readers to your website if it just consists of a single html page even called "Untitled page" and an empty menu?

Put some information in and then direct people to visit you!
GeneralWrong... Pin
Itay Sagui13-Aug-07 4:34
Itay Sagui13-Aug-07 4:34 
GeneralRe: Wrong... Pin
thund3rstruck13-Aug-07 7:12
thund3rstruck13-Aug-07 7:12 

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.