More in this section

Forums / Developing with Sitefinity / Sending a newsletter programatically

Sending a newsletter programatically

8 posts, 0 answered
  1. Andrew Wrigley
    Andrew Wrigley avatar
    63 posts
    Registered:
    01 Sep 2009
    02 Dec 2009
    Link to this post
    Hi

    I am wading through the api. I have got as far as importing a list of subscribers, creating a group and associating this group with an existing newsletter.

    Great.

    Now I want to send the newsletter.  I can't find anyway of doing this...

    Andrew
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    02 Dec 2009
    Link to this post
    Hi Andrew Wrigley,

    Below is a sample code that illustrated desired behavior.

    Telerik.Notifications.Newsletters.DataManager manager= new DataManager();
            IList subscribers = manager.GetSubscribers();
            foreach (Telerik.Newsletters.ISubscriber subscriber in subscribers)
            {
                string[] toList = new string[] { subscriber.Email };
                string body = "body"
                string subject = "subject";
                string senderName = "name";
                string senderAddress = "address";
                NewsletterManager.SendEmail(toList, body, subject, senderName, senderAddress);
            }

    You can use the following method as well SendEmailsWithDBUpdaingAsync Method

    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Chris
    Chris avatar
    3 posts
    Registered:
    27 Aug 2008
    11 Jan 2010
    Link to this post
    I'm using the SendEmailsWithDBUpdaingAsync Method, but everytime, I get a failed send.  If I duplicate the message and send it manually, it works just fine.

    Here's the code I'm using...

                INewsletter newsletter = DataManager.CreateNewsletter(); 
                newsletter.Content = Content; 
                newsletter.Subject = Subject; 
                newsletter.SendDate = DateTime.Now; 
                newsletter.DateCreated = DateTime.Now; 
                newsletter.SenderMail = "Email@Email.com"
                newsletter.SenderName = "Sender Name"
                DataManager.SaveNewsletter(newsletter); 
                DataManager.SaveGroupNewsletterRelation(newsletter.NewsletterId, FindGroupGuid()); 
     
                ISMTPSetting smtpSetting = DataManager.GetActiveSMTPSetting(); 
     
                var proxy = new MailingServiceProxy(); 
     
                foreach(var recepient in ReciptientList) 
                { 
                    var subscriber = (Telerik.Notifications.Data.GroupSubscriber) recepient; 
                    //toList.Add(subscriber.Subscriber.Email); 
                    string[] toList = new string[] { subscriber.Subscriber.Email };  
     
                    proxy.SendEmailsWithDBUpdaing(newsletter.NewsletterId, newsletter.SenderName, newsletter.SenderMail, 
                        string.Empty, toList, newsletter.Content, newsletter.Subject, smtpSetting.Host, smtpSetting.Port, 
                        smtpSetting.Username, smtpSetting.Password, smtpSetting.SSL); 
                } 

    Any idea what I'm doing wrong?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    11 Jan 2010
    Link to this post
    Hi Chris,

    I used this code, shown below without problems.

    DataManager manager = new DataManager();
     
            INewsletter newsletter = manager.CreateNewsletter();
            newsletter.SenderName = "sender@test.cc";
            newsletter.SenderMail = "sender@test.cc";
            string defaultHostEmail = "sender@test.cc";
            List<string> toList = new List<string>();
            string email = "sender@test.cc";
            toList.Add(email);
            newsletter.Content = "test";
            newsletter.Subject = "test";
            manager.SaveNewsletter(newsletter);
     
     
            ISMTPSetting smtpSetting = manager.GetActiveSMTPSetting();
            Telerik.Notifications.Services.MailingServiceProxy emailServices = new Telerik.Notifications.Services.MailingServiceProxy();
            emailServices.SendEmailsWithDBUpdaingAsync(newsletter.NewsletterId,
                           newsletter.SenderName, newsletter.SenderMail,
                           defaultHostEmail, toList.ToArray(), newsletter.Content,
                           newsletter.Subject, smtpSetting.Host, smtpSetting.Port,
                           smtpSetting.Username,
                           new CryptographyHandler().DecryptText(smtpSetting.Password), smtpSetting.SSL);


    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Chris
    Chris avatar
    3 posts
    Registered:
    27 Aug 2008
    11 Jan 2010
    Link to this post
    Copied... Pasted... No good... Same issue.  It acts like it not authenticating into the server, but sending it manually (from the Newsletter Module) works just fine.

    I'm lost... I don't know why this isn't working...  I need this working ASAP... Any other ideas?
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    12 Jan 2010
    Link to this post
    Hello Chris,

    I performed some debugging and modified the code properly. Please give a try to it

    void sendEmailBtn_Click(object sender, EventArgs e)
     {
     
         DataManager manager = new DataManager();
     
         INewsletter newsletter = manager.CreateNewsletter();
         newsletter.SenderName = "SENDER HERE";
         newsletter.SenderMail = "SENDER EMIAL";
         string defaultHostEmail = "HOST EMAIL HERE ";
         List<string> toList = new List<string>();
         // USE THE FORMAT AS SHOWN BELOW OTHERWISE THE MESSAGE WILL NOT BE SENT !!!
         string email = "FirstName LastName <your email goes here>";
         toList.Add(email);
         newsletter.Content = "test";
         newsletter.Subject = "test";
         manager.SaveNewsletter(newsletter);
     
     
         ISMTPSetting smtpSetting = manager.GetActiveSMTPSetting();
         Telerik.Notifications.Services.MailingServiceProxy emailServices = new Telerik.Notifications.Services.MailingServiceProxy();
         emailServices.SendEmailsWithDBUpdaingAsync(newsletter.NewsletterId,
                        newsletter.SenderName, newsletter.SenderMail,
                        defaultHostEmail, toList.ToArray(), newsletter.Content,
                        newsletter.Subject, smtpSetting.Host, smtpSetting.Port,
                        smtpSetting.Username,
                        new CryptographyHandler().DecryptText(smtpSetting.Password), smtpSetting.SSL);
     }


    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  7. Chris
    Chris avatar
    3 posts
    Registered:
    27 Aug 2008
    12 Jan 2010
    Link to this post
    Ok, so that seems to have gotten me part way... but now the newsletter seems like it's stuck trying to send.

    This is frustrating!

    --
    Chris
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    12 Jan 2010
    Link to this post
    Hi Chris,

    The code above works fine at my end. If you can provide some more information or just test with another settings this could shed some light.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
8 posts, 0 answered