Archive

Posts Tagged ‘Community Server’

Sending a mass email to a Communtiy Server group

April 23rd, 2009 No comments

In the control panel off Community server you are able to send a mass email to a speciff role. I have created a screen where someone can send an email to all users in a group from the control panel.  So I recently added a piece of code to community server wich enables the admin to send a mass email to a group.

Download the code here (MassEmailToGroup)

This is the code in the dll

using System;
using System.Collections.Generic;
using System.Text;
using CommunityServer;
using System.Collections.Specialized;
using CommunityServer.Components;
using CommunityServer.Hubs.Components;
using Telligent.MailGateway.Common.MailRoom;
using CommunityServer.MailRoom.Components;
using CommunityServer.Messages;
using CommunityServer.Messages.Controls;

namespace DannyCS
{
   public class Groups
   {
      public static List<string> GetGroups()
      {
         List<string> result = new List<string>();
         PagedSet<Hub> hubs = Hubs.GetHubs(false, false, false);
         foreach (Hub h in hubs.Items)
         {
            result.Add(h.Name);

         }
         return result;
      }

      public static void SendMailToGroup(string groupName, string subject, string message, bool personalMessages)
      {
         string groupApplicationKey;
         Globals.ValidateApplicationKey(groupName, out groupApplicationKey);

         Hub h = Hubs.GetHub(groupApplicationKey);
         List<User> userList = h.GetUsers(SectionMembershipType.Member);

         foreach (User u in userList)
         {
            //Sending an smpt mail to the users
            System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
            email.Subject = subject;
            email.Body = message;
            email.IsBodyHtml = u.EnableHtmlEmail;
            email.From = new System.Net.Mail.MailAddress(CSContext.Current.User.Email, CSContext.Current.SiteSettings.SiteName);
            email.To.Add(u.Email);
            Emails.QueueMessage(email);

            if (personalMessages)
            {
               //Starting a conversation between sender and reciever
               ConversationMessage conversationMessage = new ConversationMessage();
               conversationMessage.Subject = subject;
               conversationMessage.Body = message;
               if (CSContext.Current.User.UserID != u.UserID)
                  Messaging.StartConversation(conversationMessage, new int[] { CSContext.Current.User.UserID, u.UserID });
            }

         }
      }
   }
}

Also see  http://dev.communityserver.com/forums/t/506530.aspx