Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I capture the user name from active directory strip out the \\ append that to my domain name and then insert that domain name with the userid into the from field in my mail message using c#

Posted on 2010-11-24
3
Medium Priority
?
586 Views
Last Modified: 2013-11-30
protected void btnSend_Click(object sender, EventArgs e)
        {
            try

            {

                // create mail message object
                MailMessage mail = new MailMessage();
                string userName = Request.LogonUserIdentity.Name.Split('\\')[1];
                string email = userName + "@companydomain.net";


                mail.From = new MailAddress("@companydomain.net"); // -insert the strings from above into here!
                mail.To.Add("remerson@rnrc.com");                      // put To: address here
                // mail.CC = this.ddlManagerApprov.Items;             // put CC: address here
                mail.Subject = "IT Account Request Form";            // put subject here      
                mail.Body = TextBox9.Text;                          // put body of email here

                // put smtp server you will use here
                SmtpClient smtp = new SmtpClient("company email server IP"); //SmtpClient
                smtp.Send(mail);
                Label1.Text = "Thank the email was to IT.  ";
            }

            catch
            {
                Label1.Text = " Email was not sent";
            }
0
Comment
Question by:newjeep19
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 34207854
you can use
DIm UsrName as string
UsrName=System.Environment.UserName
0
 
LVL 12

Accepted Solution

by:
Anuradha Goli earned 2000 total points
ID: 34220544
0
 

Author Comment

by:newjeep19
ID: 34241769
I still need to append the user name to the domain name. This application is an internal app. In other words it is going to be used only by internal customers i.e. employess. So, instead of using Active Directory to get the user name. I found it eaiser to use Windows Authentication WA. So, the app is authenicating the user name from  single sign on and the removing the \\ before there user name. The next step is to take the user name that has already been authenticated using WA and that has had the \\ split away from the user name and appending that user name to the company's domain name i.e. company.net to the from field in the email button click event. I know that the email code is functional when I put only my company email address into the from field.
Code:
 protected void btnSend_Click(object sender, EventArgs e)
        {
            string email = String.Empty;
            string username = HttpContext.Current.User.Identity.Name.Split('\\')[1];
            MailMessage mail = new MailMessage();

            try
            {


                mail.From = new MailAddress(username +company@domain.net);
                mail.To.Add("company@domain.net");                      // put To: address here
                mail.Subject = "IT Account Request Form";            // put subject here      
                mail.Body = TextBox9.Text;                          // put body of email here

                // put smtp server you will use here
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "ip address of email server";
                smtp.Port = 25;
                smtp.Send(mail);

                Label1.Text = "Your message was successfully sent.";
            }
            catch (Exception ex)
            {
                Label1.Text = "An error occurred sending your e-mail, the error is:\r\n" + ex.Message;
            }
        }
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's recap what we learned from yesterday's Skyport Systems webinar.
We aren’t perfect, just like everyone else.  Check out the email errors our community caught and learn the top errors every email marketer should avoid.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question