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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Pop culture is prime bait for hackers seeking to infect user’s computers and mobile devices with malicious malware. Hackers know exactly what the latest trends are online and know how to use them to their advantage.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
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.

770 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