I'm trying to modify an existing project. Please excuse my lack of knowledge, our developer recently left the business and I'm trying to fix a problem until we employ a new one.
This is a webform application that sends an email based on some other information. All names are in one of the following formats:
- Firstname Lastname
- Firstname Lastname Lastname2
- Firstname Lastname-Lastname2
I am simply trying to process a string to generate an email address. Again, these follow a set format which is firstname.lastname@domain.com. I thought by finding the first space I could isolate the surname(s) and simply remove extra spaces or hyphens but my code doesn't seem to work.
The problem I am having is that anyone with more than one surname causes a delivery failure as the application gets the email address wrong. Example:
Delivery has failed to these recipients or groups: Steven.Smith (Jones@domain.com)
If it were just the name Steven Jones, it would work fine and I have tested this several times.
My code:
string fn = FullName.Text.ToString();
string y = fn.Substring(0, fn.IndexOf(" ")).Trim().Replace(" ", "");
string z = fn.Substring(fn.IndexOf(" ") + 1).Trim().Replace(" ", "");
String ToAddress = y + "." + z + "@domain.com";
The FullName variable comes from this bit of code I guess, which is in a page_load class
System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
string[] a = Context.User.Identity.Name.Split('\\');
System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]);
string Name = ADEntry.Properties["FullName"].Value.ToString();
Does anyone have any ideas on how I could achieve my goal here? I apologise for my massive lack of knowledge but I'm not entirely familiar with ASP.NET. From my research it looks like I should perhaps split the string into an array but I can't get my head around how to implement this into my program.
The original code which also didn't work was as follows. I have tried to make it more efficient and able to cope with three names in my example.
string myString1 = txtName.Text.ToString();
int t = myString1.IndexOf(" ", 0);
//Extract the first name for from address
int q = t;
string fn1 = this.txtName.Text.ToString();
string b = fn1.Substring(0,q);
//Extract the Last name for from address
int r= t;
string ln1 = this.txtName.Text.ToString();
string c= fn1.Substring(r+1);
Thanks!