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
575 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
Comment Utility
you can use
DIm UsrName as string
UsrName=System.Environment.UserName
0
 
LVL 12

Accepted Solution

by:
Anuradha Goli earned 500 total points
Comment Utility
0
 

Author Comment

by:newjeep19
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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.

762 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

12 Experts available now in Live!

Get 1:1 Help Now