Example #1
0
        /// <summary>
        /// Create an EmailCampaign object from user inputs
        /// </summary>
        /// <returns></returns>
        private EmailCampaign CreateCampaignFromInputs()
        {
            EmailCampaign campaign = new EmailCampaign();

            #region General settings

            if (!string.IsNullOrWhiteSpace(txtCampaignName.Text))
            {
                campaign.Name = txtCampaignName.Text.Trim();
            }

            if (cbCampaignType.SelectedItem != null)
            {
                //campaign.TemplateType = GetCampaignType(cbCampaignType.SelectedItem as ItemInfo);
            }

            if (!string.IsNullOrWhiteSpace(txtSubject.Text))
            {
                campaign.Subject = txtSubject.Text.Trim();
            }

            if (!string.IsNullOrWhiteSpace(txtFromName.Text))
            {
                campaign.FromName = txtFromName.Text.Trim();
            }

            if (cbFromEmail.SelectedIndex != null)
            {
                campaign.FromEmail = GetSelectedValue(cbFromEmail.SelectedItem as ItemInfo);
            }

            if (cbReplyToEmail.SelectedIndex != null)
            {
                campaign.ReplyToEmail = GetSelectedValue(cbReplyToEmail.SelectedItem as ItemInfo);
            }

            if (rbnHtml.Checked)
            {
                campaign.EmailContentFormat = CampaignEmailFormat.HTML;
            }
            else
            {
                campaign.EmailContentFormat = CampaignEmailFormat.XHTML;
            }

            if (!string.IsNullOrWhiteSpace(txtGreetingString.Text))
            {
                campaign.GreetingString = txtGreetingString.Text.Trim();
            }

            if (!string.IsNullOrWhiteSpace(txtEmailContent.Text))
            {
                campaign.EmailContent = txtEmailContent.Text.Trim();
            }

            if (!string.IsNullOrWhiteSpace(txtContent.Text))
            {
                campaign.TextContent = txtContent.Text.Trim();
            }

            #endregion General settings

            #region Message footer settings

            //organization name is required for message footer section
            if (!string.IsNullOrWhiteSpace(txtOrganizationName.Text))
            {
                MessageFooter msgFooter = new MessageFooter();

                if (!string.IsNullOrWhiteSpace(txtOrganizationName.Text))
                {
                    msgFooter.OrganizationName = txtOrganizationName.Text.Trim();
                }

                if (!string.IsNullOrWhiteSpace(txtAddressLine1.Text))
                {
                    msgFooter.AddressLine1 = txtAddressLine1.Text.Trim();
                }

                if (!string.IsNullOrWhiteSpace(txtAddressLine2.Text))
                {
                    msgFooter.AddressLine2 = txtAddressLine2.Text.Trim();
                }

                if (!string.IsNullOrWhiteSpace(txtAddressLine3.Text))
                {
                    msgFooter.AddressLine3 = txtAddressLine3.Text.Trim();
                }

                if (!string.IsNullOrWhiteSpace(txtCity.Text))
                {
                    msgFooter.City = txtCity.Text.Trim();
                }

                if (!string.IsNullOrWhiteSpace(txtZip.Text))
                {
                    msgFooter.PostalCode = txtZip.Text.Trim();
                }

                if (cbCountry.SelectedItem != null)
                {
                    msgFooter.Country = GetSelectedValue(cbCountry.SelectedItem as ItemInfo);
                }
                if (cbState.SelectedItem != null)
                {
                    var state = cbState.SelectedItem as ItemInfo;
                    if (state != null)
                    {
                        msgFooter.State = state.Name;
                    }
                }


                if (chkIncludeForwardEmail.Checked)
                {
                    msgFooter.ForwardEmailLinkText = txtIncludeForwardEmail.Text.Trim();
                    msgFooter.IncludeForwardEmail  = true;
                }

                if (chkIncludeSubscribeLink.Checked)
                {
                    msgFooter.SubscribeLinkText    = txtIncludeSubscribeLink.Text.Trim();
                    msgFooter.IncludeSubscribeLink = true;
                }

                campaign.MessageFooter = msgFooter;
            }

            #endregion Message footer settings

            #region Contact lists settings

            List <SentContactList> lists = new List <SentContactList>();

            if (lstContactList.SelectedItems.Count > 0)
            {
                foreach (var item in lstContactList.SelectedItems)
                {
                    SentContactList contactList = new SentContactList()
                    {
                        Id = GetSelectedValue(item as ItemInfo)
                    };

                    lists.Add(contactList);
                }
            }

            campaign.Lists = lists;

            #endregion Contact lists settings

            #region Schedule campaign settings

            if (rbnDraft.Checked)
            {
                campaign.Status = CampaignStatus.DRAFT;
            }
            else if (rbnSendNow.Checked)
            {
                campaign.Status = CampaignStatus.SENT;
            }
            else if (rbnScheduled.Checked)
            {
                campaign.Status = CampaignStatus.SCHEDULED;
            }

            #endregion Schedule campaign settings

            return(campaign);
        }
Example #2
0
        private EmailCampaign CreateCampaignFromInputs()
        {
            EmailCampaign campaign = new EmailCampaign();

            #region General settings

            // if (!string.IsNullOrWhiteSpace(txtCampaignName.Text))
            {
                campaign.Name = "Campaign Name here";
            }

            //  if (cbCampaignType.SelectedItem != null)
            {
                //campaign.TemplateType = GetCampaignType(cbCampaignType.SelectedItem as ItemInfo);
            }

            //   if (!string.IsNullOrWhiteSpace(txtSubject.Text))
            {
                campaign.Subject = "subject here";
            }

            //   if (!string.IsNullOrWhiteSpace(txtFromName.Text))
            {
                campaign.FromName = "FromName";
            }

            //   if (cbFromEmail.SelectedIndex != null)
            {
                campaign.FromEmail = "FromEmail";
            }

            //  if (cbReplyToEmail.SelectedIndex != null)
            {
                campaign.ReplyToEmail = "ReplyEmail";
            }

            //   if (rbnHtml.Checked)
            {
                campaign.EmailContentFormat = CampaignEmailFormat.HTML;
            }
            // else
            // {
            //     campaign.EmailContentFormat = CampaignEmailFormat.XHTML;
            //   }

            //    if (!string.IsNullOrWhiteSpace(txtGreetingString.Text))
            {
                campaign.GreetingString = "greetings";
            }

            //      if (!string.IsNullOrWhiteSpace(txtEmailContent.Text))
            {
                campaign.EmailContent = "email body";
            }

            //   if (!string.IsNullOrWhiteSpace(txtContent.Text))
            {
                campaign.TextContent = ".text content";
            }

            #endregion General settings

            //#region Message footer settings

            ////organization name is required for message footer section
            //if (!string.IsNullOrWhiteSpace(txtOrganizationName.Text))
            //{
            MessageFooter msgFooter = new MessageFooter();

            //    if (!string.IsNullOrWhiteSpace(txtOrganizationName.Text))
            //    {
            msgFooter.OrganizationName = "Org name";
            //    }

            //    if (!string.IsNullOrWhiteSpace(txtAddressLine1.Text))
            //    {
            msgFooter.AddressLine1 = "address";
            //    }

            //    if (!string.IsNullOrWhiteSpace(txtAddressLine2.Text))
            //    {
            //        msgFooter.AddressLine2 = txtAddressLine2.Text.Trim();
            //    }

            //    if (!string.IsNullOrWhiteSpace(txtAddressLine3.Text))
            //    {
            //        msgFooter.AddressLine3 = txtAddressLine3.Text.Trim();
            //    }

            //    if (!string.IsNullOrWhiteSpace(txtCity.Text))
            //    {
            msgFooter.City = "NJ";
            //    }

            //    if (!string.IsNullOrWhiteSpace(txtZip.Text))
            //    {
            msgFooter.PostalCode = "07095";
            //    }

            //    if (cbCountry.SelectedItem != null)
            //    {
            msgFooter.Country = "US";// GetSelectedValue(cbCountry.SelectedItem as ItemInfo);
            //    }
            //    if (cbState.SelectedItem != null)
            //    {
            //        var state = cbState.SelectedItem as ItemInfo;
            //        if (state != null)
            //        {
            msgFooter.State = "NJ";
            //        }
            //    }


            //    if (chkIncludeForwardEmail.Checked)
            //    {
            //        msgFooter.ForwardEmailLinkText = txtIncludeForwardEmail.Text.Trim();
            //        msgFooter.IncludeForwardEmail = true;
            //    }

            //    if (chkIncludeSubscribeLink.Checked)
            //    {
            //        msgFooter.SubscribeLinkText = txtIncludeSubscribeLink.Text.Trim();
            //   msgFooter.IncludeSubscribeLink = true;
            //    }

            campaign.MessageFooter = msgFooter;
            //}

            //#endregion Message footer settings

            #region Contact lists settings

            List <SentContactList> lists = new List <SentContactList>();

            if (contactListFromListService.Count > 0)
            {
                foreach (var item in contactListFromListService.ToList <ContactList>())
                {
                    SentContactList contactList = new SentContactList()
                    {
                        Id = item.Id
                    };

                    lists.Add(contactList);
                }
            }



            campaign.Lists = lists;

            #endregion Contact lists settings

            #region Schedule campaign settings

            //if (rbnDraft.Checked)
            //{
            //    campaign.Status = CampaignStatus.DRAFT;
            //}
            //else if (rbnSendNow.Checked)
            //{
            //  campaign.Status = CampaignStatus.SENT;
            //}
            //else if (rbnScheduled.Checked)
            //{
            campaign.Status = CampaignStatus.SCHEDULED;
            //}

            #endregion Schedule campaign settings

            return(campaign);
        }
        public void SendCampaign()
        {
            if (ValidateFields())
            {
                var confirmResult = MessageBox.Show(CommonConstants.MandatoryFields.Alert,
                                                CommonConstants.MandatoryFields.SendCampaign,
                                                MessageBoxButtons.YesNo);
                if (confirmResult == DialogResult.Yes)
                {
                    
                    string campaignId = cmbCampaign.SelectedValue.ToString();
                    // Loading panel
                    Cursor.Current = Cursors.WaitCursor;

                    try
                    {
                        ApiKey = ConstantContactAPI;
                        AccessToken = ConstantContactToken;
                        IUserServiceContext userServiceContext = new UserServiceContext(AccessToken, ApiKey);
                        var cc = new ConstantContactFactory(userServiceContext);
                        List<SentContactList> SentContactListObj = new List<SentContactList>();
                        Contact contact = new Contact();
                        List<Contact> contactList = new List<Contact>();
                        var emailCampignService = cc.CreateEmailCampaignService();
                        var result = emailCampignService.GetCampaign(campaignId);
                        int count = 0;
                        foreach (var item in result.Lists)
                                     {
                                         SentContactList contactObj = new SentContactList();
                                         contactObj.Id = item.Id;
                                         SentContactListObj.Add(contactObj);
                                     }
                                     foreach (var item in SentContactListObj)
                                     {
                                         try
                                         {
                                         var getContactDetails = cc.CreateListService();
                                         var contactDetails = getContactDetails.GetContactsFromList(item.Id,null);

                                         foreach (var j in contactDetails.Results)
                                         {
                                            
                                             string message = getMessageDetails(txtMessage.Text.Trim(), j);
                                             string phone = getPhoneDetails(cmbFieldPhone.Text.Trim(), j);

                                             if (!string.IsNullOrEmpty(phone))
                                             {
                                                // Send SMS
                                                 string smsResult = SmsSender.SendSMS(phone, NexmoFromNumber, NexmoAPI, NexmoSecretKey, HttpUtility.UrlEncode(message));
                                                 count++;
                                             }
                                         }
                                         }
                                         catch (Exception ex)
                                         {
                                            // Logger.Write(ex);
                                             continue;
                                         }
                                     }
                                    if(count>0)
                                    {
                                             MessageBox.Show(CommonConstants.MandatoryFields.Success);
                                    }
                                    else
                                    {
                                            MessageBox.Show(CommonConstants.MandatoryFields.MSGNOTSEND);
                                    }
                           
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        Cursor.Current = Cursors.Default;
                    }
                }
            }
        }
Example #4
0
        private IEnumerable <EmailCampaign> CreateCampaigns(Dictionary <string, string> parameters)
        {
            XDocument emailTemplate = new XDocument();

            using (System.Xml.XmlWriter writer = emailTemplate.CreateWriter())
            {
                XslCompiledTransform emailTemplateTransform = new XslCompiledTransform();
                XsltArgumentList     emailTemplateArgList   = new XsltArgumentList();
                foreach (KeyValuePair <string, string> parameter in parameters)
                {
                    emailTemplateArgList.AddParam(parameter.Key, "", parameter.Value);
                }
                emailTemplateTransform.Load(_emailTemplateTransformUrl);
                emailTemplateTransform.Transform(createEmailConnectionXML().CreateReader(), emailTemplateArgList, writer);
            }
            List <EmailCampaign> campaigns = new List <EmailCampaign>();

            foreach (XElement campaignElement in emailTemplate.Descendants("campaign"))
            {
                EmailCampaign campaign = new EmailCampaign();
                campaign.EmailContentFormat = CampaignEmailFormat.XHTML;
                campaign.Status             = CampaignStatus.SENT;
                foreach (XElement property in campaignElement.Elements("property"))
                {
                    string name  = property.Attribute("name").Value;
                    string value = String.Concat(property.Nodes());
                    try
                    {
                        campaign.GetType().GetProperty(name).SetValue(campaign, value, null);
                    }
                    catch (Exception)
                    {
                        throw new Exception($"Error with campaign property setting, {name}, with value, {value}");
                    }
                }
                MessageFooter msgFooter = new MessageFooter();
                campaign.MessageFooter = msgFooter;
                foreach (XElement method in emailTemplate.Descendants("footer").Elements("property"))
                {
                    string name  = method.Attribute("name").Value;
                    string value = method.Value;
                    try
                    {
                        msgFooter.GetType().GetProperty(name).SetValue(msgFooter, value, null);
                    }
                    catch (Exception)
                    {
                        throw new Exception($"Error with footer property setting, {name}, with value, {value}");
                    }
                }
                List <SentContactList> lists = new List <SentContactList>();
                foreach (XElement method in emailTemplate.Descendants("contacts").Elements("contact"))
                {
                    string          id          = method.Attribute("id").Value;
                    SentContactList contactList = new SentContactList()
                    {
                        Id = id
                    };
                    lists.Add(contactList);
                }
                campaign.Lists = lists;
                campaigns.Add(campaign);
            }
            return(campaigns);
        }