Example #1
0
        public Return AddUpdateEmailAddress(string listId, User user, Dictionary <string, object> mergeFields = null, List <string> addTags = null)
        {
            var returnObj = new Return();

            try
            {
                // Use the Status property if updating an existing member

                var memberExists = MailChimpManager.Members.ExistsAsync(listId, user.EmailAddress).Result;

                Member member = null;

                if (memberExists)
                {
                    member = MailChimpManager.Members.GetAsync(listId, user.EmailAddress).Result;                     //new Member { EmailAddress = user.EmailAddress, Status = Status.Subscribed, StatusIfNew = Status.Subscribed };
                }
                else
                {
                    member = new Member {
                        EmailAddress = user.EmailAddress, Status = Status.Subscribed, StatusIfNew = Status.Subscribed
                    };
                }

                if (mergeFields != null)
                {
                    foreach (var mergeField in mergeFields)
                    {
                        member.MergeFields.Add(mergeField.Key, mergeField.Value);
                    }
                }

                if (addTags != null)
                {
                    var listSegments = MailChimpManager.ListSegments.GetAllAsync(listId).Result;
                    var addSegments  = listSegments.Where(i => addTags.Contains(i.Name));

                    foreach (var currentTag in member.Tags)
                    {
                        if (!addSegments.Any(i => i.Id == currentTag.Id))
                        {
                            MailChimpManager.ListSegments.DeleteMemberAsync(listId, currentTag.Id.ToString(), member.EmailAddress);
                        }
                    }

                    foreach (var segment in addSegments)
                    {
                        MailChimpManager.ListSegments.AddMemberAsync(listId, segment.Id.ToString(), member);
                    }
                }

                /*var birthDay = new DateTime(DateTime.Now.Year, user.Month, user.Day);
                 *
                 * member.MergeFields.Add("FNAME", user.Name);
                 * member.MergeFields.Add("CITY", user.City);
                 * member.MergeFields.Add("BIRTHDAY", birthDay.ToString("MM/dd"));
                 * member.MergeFields.Add("REGION", user.Region);*/

                //member.MergeFields.Add("LNAME", user.LastName);

                member = MailChimpManager.Members.AddOrUpdateAsync(listId, member).Result;

                if (addTags != null)
                {
                    member = AddTagsToExistingMember(member, listId, addTags);
                }

                returnObj.SetRawData(member);

                return(returnObj);
            }
            catch (Exception ex)
            {
                returnObj.Error = ErrorHelper.CreateError(ex.Message, ex.InnerException?.Message);
                return(returnObj);
            }
        }
Example #2
0
        public static Return Send(string fromEmailAddress, IEnumerable <MailAddress> emailAddresses, string subject, string body, EmailMode emailMode = EmailMode.Both, bool bcc = true)
        {
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

            Return returnObj = new Return();
            var    emailLog  = new EmailLog();

            if (emailMode == EmailMode.Both || emailMode == EmailMode.Smtp)
            {
                try
                {
                    MailMessage message = new MailMessage();

                    foreach (MailAddress address in emailAddresses)
                    {
                        if (bcc)
                        {
                            message.Bcc.Add(address);
                        }
                        else
                        {
                            message.To.Add(address);
                        }
                    }

                    message.Sender = new MailAddress(fromEmailAddress);

                    message.IsBodyHtml = true;
                    message.Subject    = subject;
                    message.Body       = body;

                    emailLog = GetEmailLogFromMailMessage(message);

                    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

                    client.Send(message);

                    emailLog.ServerMessage = "Successfully sent email";

                    EmailsMapper.Insert(emailLog);

                    return(returnObj);
                }
                catch (Exception ex)
                {
                    ErrorHelper.LogException(ex);

                    returnObj.Error = ErrorHelper.CreateError(ex);

                    emailLog.ServerMessage = returnObj.Error.Message;

                    EmailsMapper.Insert(emailLog);

                    if (emailMode == EmailMode.Both)
                    {
                        var directSentReturn = SendDirectMessage(fromEmailAddress, emailAddresses, subject, body, bcc);

                        if (directSentReturn.IsError)
                        {
                            return(directSentReturn);
                        }
                        else
                        {
                            returnObj = BaseMapper.GenerateReturn();
                        }
                    }

                    return(returnObj);
                }
            }
            else
            {
                return(SendDirectMessage(fromEmailAddress, emailAddresses, subject, body));
            }
        }
Example #3
0
        public Return PublishLive()
        {
            var returnObj    = new Return();
            var liveVersion  = BaseMapper.GetObjectFromContext((MediaDetail)this.Media.GetLiveMediaDetail());
            var selectedItem = BaseMapper.GetObjectFromContext((MediaDetail)this);

            selectedItem.HistoryVersionNumber  = 0;
            selectedItem.HistoryForMediaDetail = null;
            selectedItem.IsDraft     = false;
            selectedItem.PublishDate = DateTime.Now;
            //selectedItem.ShowInMenu = true;

            IEnumerable <MediaDetail> items = new List <MediaDetail>();

            if (liveVersion != null)
            {
                items = liveVersion.History.ToList();

                foreach (var item in items)
                {
                    if (item.ID != selectedItem.ID)
                    {
                        var tmpItem = BaseMapper.GetObjectFromContext(item);
                        item.HistoryForMediaDetailID = selectedItem.ID;
                    }
                }
            }

            foreach (var fieldAssociation in selectedItem.FieldAssociations)
            {
                var index = 1;
                foreach (var history in fieldAssociation.MediaDetail.History)
                {
                    history.HistoryForMediaDetail = fieldAssociation.MediaDetail;
                    history.HistoryVersionNumber  = 1;

                    index++;
                }

                fieldAssociation.MediaDetail.HistoryForMediaDetail = null;
                fieldAssociation.MediaDetail.HistoryVersionNumber  = 0;
            }

            foreach (var field in selectedItem.Fields)
            {
                foreach (var fieldAssociation in field.FieldAssociations)
                {
                    var index = 1;

                    foreach (var mediaDetail in fieldAssociation.MediaDetail.Media.MediaDetails)
                    {
                        mediaDetail.HistoryForMediaDetail = fieldAssociation.MediaDetail;
                        mediaDetail.HistoryVersionNumber  = 1;

                        index++;
                    }

                    fieldAssociation.MediaDetail.HistoryForMediaDetail = null;
                    fieldAssociation.MediaDetail.HistoryVersionNumber  = 0;
                }

                if (liveVersion != null)
                {
                    field.FrontEndSubmissions = liveVersion.LoadField(field.FieldCode)?.FrontEndSubmissions;
                }
            }

            foreach (var mediaTypeField in selectedItem.MediaType.Fields)
            {
                if (!selectedItem.Fields.Any(i => i.FieldCode == mediaTypeField.FieldCode))
                {
                    var mediaDetailField = new MediaDetailField();
                    mediaDetailField.CopyFrom(mediaTypeField);

                    mediaDetailField.UseMediaTypeFieldFrontEndLayout = true;
                    mediaDetailField.UseMediaTypeFieldDescription    = true;

                    mediaDetailField.MediaTypeField = mediaTypeField;

                    mediaDetailField.DateCreated = mediaDetailField.DateLastModified = DateTime.Now;

                    mediaDetailField.OrderIndex = selectedItem.Fields.Count;
                    selectedItem.Fields.Add(mediaDetailField);
                }
            }


            if (liveVersion != null)
            {
                if (items.Any())
                {
                    liveVersion.HistoryVersionNumber = items.OrderByDescending(i => i.HistoryVersionNumber).FirstOrDefault().HistoryVersionNumber + 1;
                }
                else
                {
                    liveVersion.HistoryVersionNumber = 1;
                }

                liveVersion.HistoryForMediaDetail = (MediaDetail)selectedItem;


                var associations = BaseMapper.GetDataModel().FieldAssociations.Where(i => i.AssociatedMediaDetailID == liveVersion.ID);

                foreach (var association in associations)
                {
                    association.MediaDetail = (MediaDetail)selectedItem;
                }
            }

            returnObj = MediaDetailsMapper.Update(selectedItem);

            if (!returnObj.IsError)
            {
                if (liveVersion != null)
                {
                    liveVersion.HistoryForMediaDetailID = selectedItem.ID;
                    returnObj = MediaDetailsMapper.Update(liveVersion);
                }

                if (!returnObj.IsError)
                {
                    ContextHelper.Clear(ContextType.Cache);
                    FileCacheHelper.ClearAllCache();

                    return(returnObj);
                }
                else
                {
                    return(returnObj);
                }
            }
            else
            {
                return(returnObj);
            }
        }
Example #4
0
        private static Return SendDirectMessage(string fromEmailAddress, IEnumerable <MailAddress> emailAddresses, string subject, string body, bool bcc = true)
        {
            var returnObj = new Return();
            var emailLog  = new EmailLog();

            Message message = new Message();

            message.Subject = subject;
            message.From    = new Address(fromEmailAddress);

            foreach (var mailAddress in emailAddresses)
            {
                if (bcc)
                {
                    message.Bcc.Add(mailAddress.Address);
                }
                else
                {
                    message.To.Add(mailAddress.Address);
                }
            }


            message.IsHtml        = true;
            message.BodyHtml.Text = body;
            message.BodyText.Text = body;

            emailLog.FromEmailAddress   = message.From.Email;
            emailLog.SenderName         = message.Sender.Name;
            emailLog.SenderEmailAddress = message.Sender.Email;
            emailLog.Subject            = message.Subject;
            emailLog.ToEmailAddresses   = "";
            emailLog.VisitorIP          = "";
            emailLog.RequestUrl         = "";
            emailLog.ServerMessage      = "";
            emailLog.Message            = body;

            try
            {
                var mailMessage = new MailMessage();
                mailMessage.CopyFrom(message);

                var returnStr = ActiveUp.Net.Mail.SmtpClient.DirectSend(message);

                if (!string.IsNullOrEmpty(returnStr))
                {
                    emailLog.ServerMessage = returnObj?.Error?.Message;

                    if (emailLog.ServerMessage == null)
                    {
                        emailLog.ServerMessage = "";
                    }

                    EmailsMapper.Insert(emailLog);
                }

                return(returnObj);
            }
            catch (Exception ex)
            {
                ErrorHelper.LogException(ex);

                returnObj.Error = ErrorHelper.CreateError(ex);

                emailLog.ServerMessage = returnObj?.Error?.Message;

                if (emailLog.ServerMessage == null)
                {
                    emailLog.ServerMessage = "";
                }

                EmailsMapper.Insert(emailLog);

                return(returnObj);
            }
        }