There are several different situations that I have come across where there is a need to send an email to all of the members of a SharePoint group. This first came up internally when several departments wanted this feature available on their team sites. Users Just wanted to be able to click somewhere on their site and have Outlook open with an email addressed to the members of their team. We were able to find a suitable solution from Advis that did pretty much what we wanted, at least for a while. After using it for months, we found one large flaw that drove me to make my own version… It limited the total size of the string of email addresses being sent to outlook to 255 characters.
In other words, when outlook opened, the To field would be populated with the email addresses of the group of site members, but would be cut off at 255 total characters if there were a lot of members in the site. So, I decided to build my own version and solve that problem while adding a bit more functionality.

I added three options in the Editor Part.
1) Email All Site Collection Users (SPSite)
2) Email All SubSite Users (SPWeb)
3) Show All Site Security Groups
I will go over the code in some detail below and make the full code (and .wsp file) available here as I intend to do with all Web Parts I post here, but before I do I want to point out one very important thing about this Web Part. It makes use of Elevated Privileges which I went over here to pull information about the SharePoint security groups. So this allows for every user that can get to a page with this web part on it to email the members of groups that they normally may not be allowed to see. Essentially allowing them to see who is in each group. This was fine in my situation, but if you intend on using it as-is be aware of that. Alternatively, as I provide the code you could change that if you don’t like it. OK, moving on…
This Web Part only has two classes, the main web part class and a custom editor class. It is about 400 total lines of code, so I will only go over the important parts here in the blog, the rest can be downloaded here. There are three buttons that each have their own function attached to them (Mail To, CC To and BCC To) that are all very similar. I will go over the function called when Mail To is clicked below.
void mailToButton_Click
(object sender, EventArgs e
)
{
try
{
SPSite siteColl
= SPContext
.Current.Site;
SPWeb site
= SPContext
.Current.Web;
SPSecurity
.RunWithElevatedPrivileges(delegate()
{
using (SPSite ElevatedsiteColl
= new SPSite
(siteColl
.ID))
{
using (SPWeb ElevatedWeb
= ElevatedsiteColl
.OpenWeb(site
.ID))
{
SPGroupCollection groups
= ElevatedWeb
.Groups;
SPGroup group
;
to
= "";
if (siteUsersCheckBox
.Items[0].Selected)
{
foreach (SPUser user
in ElevatedWeb
.SiteUsers)
{
if (user
.Email != null && user
.Email != "")
to
+= user
.Name + " <" + user
.Email + ">;";
}
}
if (webUsersCheckBox
.Items[0].Selected)
{
foreach (SPUser user
in ElevatedWeb
.AllUsers)
{
if (user
.Email != null && user
.Email != "")
to
+= user
.Name + " <" + user
.Email + ">;";
}
}
foreach (ListItem listItem
in groupCheckBox
.Items)
{
if (listItem
.Selected == true)
{
group
= groups
[listItem
.Value];
foreach (SPUser user
in group
.Users)
{
if (user
.Email != null && user
.Email != "")
to
+= user
.Name + " <" + user
.Email + ">;";
}
}
}
}
}
});
if (to
!= "")
{
string response
= "<script TYPE='text/javascript'>window.location='mailto:" + to
+ "';</script>";
Context
.Response.Write(response
);
}
groupCheckBox
.ClearSelection();
siteUsersCheckBox
.ClearSelection();
webUsersCheckBox
.ClearSelection();
}
catch (Exception ex
)
{
this.Controls.Add(new LiteralControl
(ex
.Message));
}
}
As you can see, the first part of the code above is checking to see what groups were selected, and building the To string accordingly. This is pretty straight forward and there are a couple of different ways you could get at the user email addresses, but this is my preferred method. The last part of the code is the very simple, yet interesting part in that it allowed me to remove the 255 character constraint of the other version. I built a little javascript with a mailto link in it, that will open up the default email client of the user (Outlook in my case) and construct a blank email with the To address filled in.
if (to != "")
{
string response = "<script TYPE='text/javascript'>window.location='mailto:" + to + "';</script>";
Context.Response.Write(response);
}
The only problem (if it is a problem) that I can find with my solution is if someone refreshes the page after clicking one of the web part’s buttons. In this case another email message will be opened as it is still in the postback of the page.
The editor class is straight forward, listing three choices which I outlined above. Please feel free to take a look over it and use it if you see fit. Thanks and enjoy.