1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Bulk adding emails to newsletters

Bulk adding emails to newsletters

2 posts, 0 answered
  1. Simon
    Simon avatar
    2 posts
    Registered:
    26 Jan 2009
    13 Feb 2009
    Link to this post
    Morning

    Is there a way to add, for example, 1,000 emails in one go as subscribers to a newsletter?

    I can't currently see how to do this in the demo or documentation.

    Cheers
    Simon
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    13 Feb 2009
    Link to this post
    Hi Simon,

    Thank you for using our services.

    Here is my approach.

    First you need to create a file, let's say a cvs file with users email addresses. You can add first name, last name etc. Then you can create a use control that will parse the data from the cvs file.
    You should use CreateSubscriber method (http://www.sitefinity.com/help/developer-manual/telerik.notifications-telerik.notifications.newsletters.datamanager-createsubscriber.html) to create a new subscriber.
    Additionally you could also specify a Group where the users will be added.

    Sample:

    <asp:TextBox ID="absoluteURLtoCSV" runat="server"  
        Text="YOUR FILE HERE" Width="368px" /> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <br /><br /><br /><br /> 
    Choose a Subsciber Group in which to import users<p> 
        &nbsp;</p> 
    &nbsp;<asp:TextBox ID="subscriberGroupTextBox" runat="server"  
        Text="Subscriber Group" Width="368px" /> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="import" /> 

    code behind

        // set buffer size 
        public const int BUFFER_SIZE = 4096 ; 
     
        protected void Page_Load(object sender, EventArgs e) 
        { 
        } 
        protected void Button1_Click(object sender, EventArgs e) 
        { 
            // new instance of DataMAnager 
            DataManager dm = new DataManager(); 
            IGroup subscriberGroup = null ; 
            // get all groups          
            IList subscriberGroups = dm.GetGroups(""); 
            int i = 0; 
            // check whether the subscriber group exists. 
            while (subscriberGroup == null && i < subscriberGroups.Count) 
            {    
                // get the subscriber group value from the textbox 
                if (((IGroup)subscriberGroups[i]).GroupName == subscriberGroupTextBox.Text) 
                { 
                    subscriberGroup = (IGroup)subscriberGroups[i]; 
                } 
                i++; 
            } 
            if (subscriberGroup != null
            {  
                // get the cvs file 
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(absoluteURLtoCSV.Text); 
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); 
                // read the file 
                MemoryStream memoryStream = null
                byte[] buffer = new byte[BUFFER_SIZE]; 
                Stream responseStream = webResponse.GetResponseStream(); 
                try 
                { 
                    int readBytes; 
                    memoryStream = new MemoryStream(); 
                    while ((readBytes = responseStream.Read(buffer, 0, BUFFER_SIZE)) > 0) 
                    { 
                        memoryStream.Write(buffer, 0, readBytes); 
                    } 
                    string csvContents = 
                        Encoding.UTF8.GetString(memoryStream.GetBuffer(), 0, (int)memoryStream.Length); 
     
                    string[] subscribersRows = csvContents.Split('\n'); 
                    foreach (string subscriberRow in subscribersRows) 
                    { 
                        string subscriberInfoRow = subscriberRow.Trim(); 
                        if (!string.IsNullOrEmpty(subscriberInfoRow)) 
                        { 
                            string[] columns = subscriberInfoRow.Split(','); 
     
                            string userEmail = columns[0].Trim(); 
                          // create a new users here using CreateSubscriber method 
                            ISubscriber subscriber = dm.CreateSubscriber(userEmail); 
                            // get the first name from the cvs file
                            subscriber.FirstName = columns[1].Trim();
                            // get the last name from the cvs file
                            subscriber.LastName = columns[2].Trim(); 
                            dm.SaveSubscriber(subscriber); 
                            IGroupSubscriber grSubs = 
                                dm.CreateGroupSubscriberRelation(subscriberGroup.GroupId, subscriber.SubscriberId); 
                            dm.SaveGroupSubscribers(grSubs); 
                        } 
                    } 
                } 
                finally 
                { 
                    if (memoryStream != null
                        memoryStream.Close(); 
                } 
            } 
            else 
            { 
                Response.Write(string.Format("The subscriber group {0} does not exist!", subscriberGroupTextBox.Text)); 
            } 
     
        } 

    I hope this helps.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
2 posts, 0 answered