How to send email to People or group field user
People/Group field in SharePoint allow us to keep adding the existing user(s) or group(s) into the document library or list item. Let’s think, I have a list where one field name i.e. Assigned To (reviewer) is type of People/Group. Objective is when I assign someone to review the list item, and then an email notification needs to be fired up to that reviewer.
In other words, you have to find that person from the UserCollection list in SharePoint and send an email to that associated person.
Now once you assign one person on this field, you will found how the SharePoint display his/her name in the New/Edit OTB form and it is like DOMAIN\LOGINNAME. So if your domain name is SP-SERV and your name is Himadrish Laha, it will be looks like SP-SERV\Himadrish Laha.
Below the simple code I have used to fetch the string data and it returns me something else which I found in the debugging mode “1;#SP-SERV\\kiran” (here I have added user Kiran). When I have added my name it looks like “16;#SP-SERV\\Himadrish Laha“. String represent is kind of <UserID>;#<UserLoginName>.More difficult for you to work.
My initial thought is to split the string value and use the UserID. However latter I found this is not the actual UserID associated with the user, instead of this, it is kind of index ID associated with this user in SharePoint. So, if you try to use this ID as below, I bet you will found different user in SPUser object. So the following code is incorrect to trap actual user:
string UserAccount = _ListItem["Assigned To (Reviewer)"].ToString();
int UserID;
if (UserAccount.Trim() != string.Empty)
{
UserID = Convert.ToInt32(UserAccount.Substring(0, UserAccount.IndexOf(“;”)));
SPUser _SPUser = properties.OpenWeb().Users[UserID];
string UserEmail = _SPUser.Email.ToString();
}
What we have left now is only the login display name. Here is also one drawback to trap the user. This string is Login Display Name and it is not the LoginID. So if my login id is Himadrish.Laha and my display name is Himadrish Laha, it will show me Himadrish Laha in the People filed control text box. Note the missing of the DOT (.). For people having same string in loginID as well as Login Display name shouldn’t be a problem, but for other it will make a big difference.
To resolve this issue, the following code may help you. In most of the cases, you can simply add one DOT (.) in between user’s given name and his/her family name. Inside the code, the web represent the SPWeb object. So the method is being used here is SPWeb.EnsureUser(). You need to pass user’s login id over there, and it will return you SPUser object.
string UserAccount = _ListItem["Assigned To (Reviewer)"].ToString();
string UserLogin;
string UserEmail = string.Empty;
if (UserAccount.Trim() != string.Empty)
{
UserLogin = UserAccount.Substring(UserAccount.IndexOf(“#”), (UserAccount.Length – UserAccount.IndexOf(“#”)));
UserLogin = UserLogin.Replace(“#”, “”).Replace(“;”, “”);
UserLogin = @”SP-SERV\” + UserLogin.Replace(” “,”.”);
SPUser _SPUser = web.EnsureUser(UserLogin);
UserEmail = _SPUser.Email.ToString();
_SPUser = null;
}
Kindly note, the above code may need to change according to get the login id from the login name. If in your organization the login id is something the first character of your given name i.e. H for Himadrish and then your last name i.e. Laha, then your login id would be HLAHA. So you need to change the above code and found the actual login id what it would be. Rest of the line would be same.
Hopes this will help you.


this post is very usefull thx!
My cousin recommended this blog and she was totally right keep up the fantastic work!
I wish we can Google how a certain person feels about us.
Great post.