Click here to Skip to main content
Licence 
First Posted 13 Aug 2007
Views 21,697
Downloads 43
Bookmarked 21 times

Query Exchange server and retrive email address from NT username

By | 13 Aug 2007 | Article
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:

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

About the Author

rogwabbit5

Database Developer

United States United States

Member

DB2 / MSSQL Database Administrator / PeopleSoft Administrator

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionYour website??? PinmemberAndreas Kroll3:28 21 Aug '07  
GeneralWrong... PinmemberItay Sagui4:34 13 Aug '07  
GeneralRe: Wrong... Pinmemberthund3rstruck7:12 13 Aug '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 13 Aug 2007
Article Copyright 2007 by rogwabbit5
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid