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
577 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
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 500 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
OUTLOOK.COM Inbox fonts appearing ornate 14 79
Super Scope, DHCP 5 49
Create New Attributes in Active Directory Schema 9 39
If-Then-Else ASP problem 6 46
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now