Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Query Exchange server and retrive email address from NT username

0.00/5 (No votes)
13 Aug 2007 1  
This is a simple methog to query an exchange server and retrive a remote users email address on a corporate intranet

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:

//

// 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