Alex Alex - 1 month ago 12
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 + "@test.domain.com";
string newmail = GetCheckedMail(email, mventry);

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


//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 "";
}


Problem:
When I run sync cycle for first time I get normal email like
duplicateuser1@test.domain.com
For next sync cycle this emails are updated to
duplicateuser@test.domain.com1

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

Can anybody say why it is happens?
Thanks!

Answer

The problem is the line:

checkedmailName = checkedmailName + nameSuffix.ToString();

checkedmailName has a value like this: firstName.lastName@test.domain.com

So, you're doing this:

checkedmailName = firstName.lastName@test.domain.com + 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!

Comments