Reputation: 1640
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 [email protected]. 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 ([email protected])
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!
Upvotes: 0
Views: 2186
Reputation: 841
It's not entirely clear what format the email address of one of your users with two last names should have, but it seems like you should be able to do
//Assuming example input "Stephen Smith-Jones" or "Stephen Smith Jones"
//split on spaces and hyphens
string[] names = FullName.Text.ToString()
.Split(new string[]{" ", "-"},
StringSplitOptions.RemoveEmptyEntries); //in case of extra spaces?
//[email protected]
var email = String.Format("{0}.{1}{2}@domain.com",
names[0],
names[1],
(names.Length > 2) ? names[2] : "");
What's it do? Breaks your full name into an array of names. Takes the first name, the second name, and (if there is one) the third name and puts them into the email. If you want to deal with middle names or other variants, you'll need to be more sophisticated.
EDIT: reflects the [firstname].[lastname][lastname2?]@domain.com format
Upvotes: 1
Reputation: 49988
You can use regular expressions and groupings:
Regex r = new Regex("(\\w+) (\\w+)[ -]?(\\w+)?");
string name = "John Anderson Johnson";
Match m = r.Match(name);
if(m.Success)
{
string first_name = m.Groups[1].Value;
string last_name = m.Groups[2].Value;
string last_name2 = ((m.Groups.Count > 2 && !m.Groups[3].Value.Equals(String.Empty))
? m.Groups[3].Value : String.Empty);
// format as desired
string email = String.Format("{0}.{1}{2}@domain.com", first_name, last_name2);
string email2 = String.Format("{0}.{1}@domain.com", first_name, last_name);
Console.WriteLine(email); // outputs: [email protected]
Console.WriteLine(email2); // outputs: [email protected]
}
Updated Source updated to handle op comment:
alias entered which is in the format of: [email protected] (no hyphen)
Upvotes: 0
Reputation: 1133
You can first clean your string like this (I took it from this thread):
string cleanedString = System.Text.RegularExpressions.Regex.Replace(dirtyString,@"\s+"," ");
Then you can replace white-spaces and hyphens with dots:
cleanedString = cleanedString.Trim().Replace(" ", ".").Replace("-", ".");
Johan Doe Doe2 will then be John.Doe.Doe2
After that you can remove the last part with this:
if (cleanedString.IndexOf('.') != cleanedString.LastIndexOf('.'))
{
cleanedString = cleanedString.Substring(0, cleanedString.LastIndexOf('.'));
}
The result will then be John.Doe
You could probably do it in a better way...
Upvotes: 0
Reputation: 1455
you do not replace the hyphens:
Replace
string z = fn.Substring(fn.IndexOf(" ") + 1).Trim().Replace(" ", "");
by
string z = fn.Substring(fn.IndexOf(" ") + 1).Trim().Replace(" ", "").Replace("-", "");
Additionally you should check for invalid input at all and for invalid Mail-Adress-Characters. This could be an accented char as é for example...
Upvotes: 0
Reputation: 350
Something like this?
string fn = FullName.Text.ToString();
string forename = fn.Split(' ')[0];
string toAddress = forename + "." + fn.Substring(forename.Length + 1) + "@domain.com";
Upvotes: 1