Alex Alex - 1 year ago 211
C# Question

Generate unique email in c# for Microsoft Identity Manager

I have a DB ad Microsoft Identity Manager to generate user accounts from HR to MS Active Directory and so on.

I have a such code for generate unique email:

case "mailgenerate":
if (mventry["email"].IsPresent)
// Do nothing, the mail was already generated.


if (csentry["FIRST"].IsPresent && csentry["LAST"].IsPresent);
string FirstName = replaceRUEN(csentry["FIRST"].Value);
string LastName = replaceRUEN(csentry["LAST"].Value);
string email = FirstName + "." + LastName + "";
string newmail = GetCheckedMail(email, mventry);

if (newmail.Equals(""))
throw new TerminateRunException("A unique mail could not be found");
mventry["email"].Value = newmail;

//Generate mail Name method
string GetCheckedMail(string email, MVEntry mventry)
MVEntry[] findResultList = null;
string checkedmailName = email;
for (int nameSuffix = 1; nameSuffix < 100; nameSuffix++)
//added ; and if corrected
findResultList = Utils.FindMVEntries("email", checkedmailName,1);

if (findResultList.Length == 0)
// The current mailName is not in use.
return (checkedmailName);
MVEntry mvEntryFound = findResultList[0];
if (mvEntryFound.Equals(mventry))
return (checkedmailName);
// If the passed email is already in use, then add an integer value
// then verify if the new value exists. Repeat until a unique email is checked
checkedmailName = checkedmailName + nameSuffix.ToString();
// Return an empty string if no unique mailnickName could be created.
return "";

When I run sync cycle for first time I get normal email like
For next sync cycle this emails are updated to

This code I'm also using to generate mailnickname and accountname without any problems.

Can anybody say why it is happens?

Answer Source

The problem is the line:

checkedmailName = checkedmailName + nameSuffix.ToString();

checkedmailName has a value like this:

So, you're doing this:

checkedmailName = + 1;

You need to do something like this:

checkedmailName = checkedmailName.Split('@')[0] + nameSuffix.ToString()+ "@" + checkedmailName.Split('@')[1];

Whith this, you're getting the part before @, adding a int value and then, appending the @+ domain.

Updated by author of thread I changed split -> Split and it works. Thanks!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download