예제 #1
0
        private bool UpdatePromotionalPricing(User user, Price price)
        {
            try
            {
                UserPrice uPrice = crumbsRepository.FindUserPriceByUserID(user.UserID, price.Type);
                if (uPrice == null)
                {
                    // no current promotions for this user - create a new one
                    UserPrice newUserPrice = new UserPrice()
                    {
                        User = user,
                        Price = price,
                        Subscribed = 0
                    };
                    crumbsRepository.InsertOrUpdateUserPrice(newUserPrice);
                }
                else if (uPrice.Subscribed == 0)
                {
                    if (uPrice.Price.Expiration < DateTime.Now)
                    {
                        // expired promotion found - replace it
                        uPrice.Price = price;
                        uPrice.Amount = 0;
                        uPrice.PaymentID = "";
                        crumbsRepository.InsertOrUpdateUserPrice(uPrice);
                    }
                    else
                    {
                        // un-expired promotion found - keep it
                        return false;
                    }
                }
                else
                {
                    // already subscribed - leave it alone
                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception is:" + ex.ToString());
                return false;
            }

            return true;
        }
예제 #2
0
        public static bool ProcessOrder(ICrumbsRepository crumbsRepository, Invoice invoice, string serverPath, string email, bool updateCopies)
        {
            List<MidiFile> files = new List<MidiFile>();
            User user = (crumbsRepository as IUserRepository).FindUserByID(invoice.UserID);
            Mailing mail = crumbsRepository.AllMailings.SingleOrDefault(item => item.Name == "Send_MIDI");

            if (user.UserID == 20)
            {
                User newUser = (crumbsRepository as IUserRepository).FindUserByEmail(email);
                if (newUser == null)
                {
                    newUser = new User()
                    {
                        Type = 3,
                        UserKey = email,
                        UserName = email,
                        Email = email,
                        CreateDate = DateTime.Now,
                        LastLoginDate = DateTime.Now
                    };
                    (crumbsRepository as IUserRepository).InsertOrUpdateUser(newUser);
                    crumbsRepository.Save();
                }
                user = newUser;
            }

            if (user == null || mail == null)
                return false;

            string userEmail = user.Email;
            if (string.IsNullOrEmpty(userEmail))
                userEmail = email;

            if (string.IsNullOrEmpty(userEmail))
                return false;

            SmtpClient client = null;
            try
            {
                bool useGMailForOrders = false;
                string s = ConfigurationManager.AppSettings["UseGMailForOrders"];
                if (string.IsNullOrEmpty(s) == false)
                {
                    useGMailForOrders = Convert.ToBoolean(s);
                }

                if (useGMailForOrders == true)
                {
                    client = new SmtpClient("smtp.gmail.com", 587);
                    client.EnableSsl = true;
                    client.Timeout = 10000;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.UseDefaultCredentials = false;
                    client.Credentials = new NetworkCredential("*****@*****.**", "Crumbs123");
                }
                else
                {
                    client = new SmtpClient("m02.internetmailserver.net", 587);
                    client.EnableSsl = false;
                    client.Credentials = new NetworkCredential("*****@*****.**", "Crumbs123", "");
                }

                MailAddress from = new MailAddress("*****@*****.**", "PianoCrumbs.com");
                MailAddress to = new MailAddress(userEmail, "");
                MailMessage message = new MailMessage(from, to);

                message.Subject = mail.Subject;
                message.IsBodyHtml = true;
                message.CC.Add(new MailAddress("*****@*****.**", "PianoCrumbs.com"));

                if (invoice.MidiID.HasValue)
                {
                    MidiFile file = crumbsRepository.FindMidiFile(invoice.MidiID.Value);
                    if (file != null)
                    {
                        string path = MidiFileHelper.getMidiPath(file);
                        string FullPath = serverPath + path;

                        Attachment attachment = new Attachment(FullPath);
                        attachment.Name = file.FileName;
                        message.Attachments.Add(attachment);

                        files.Add(file);
                    }
                }
                else if (invoice.PackageID.HasValue)
                {
                    Package package = crumbsRepository.FindPackage(invoice.PackageID.Value);
                    if (package != null)
                    {
                        foreach (MidiFile file in package.MidiFiles)
                        {
                            string path = MidiFileHelper.getMidiPath(file);
                            string FullPath = serverPath + path;

                            Attachment attachment = new Attachment(FullPath);
                            attachment.Name = file.FileName;
                            message.Attachments.Add(attachment);

                            files.Add(file);
                        }
                    }
                }
                message.Body = string.Format(mail.Text, user.UserName, "", "", "");

                client.Send(message);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    invoice.Comment = ex.InnerException.Message + " | " + DateTime.Now.ToString("s");
                }
                else
                {
                    invoice.Comment = ex.Message + " | " + DateTime.Now.ToString("s");
                }
                crumbsRepository.InsertOrUpdateInvoice(invoice);
                crumbsRepository.Save();

                return false;
            }

            // Create new SentMail record
            SentMail sent = new SentMail();
            sent.UserID = user.UserID;
            sent.MailID = mail.ID;
            sent.Date = DateTime.Now;
            crumbsRepository.InsertOrUpdateSentMail(sent);

            if (user.Role == null && updateCopies == true)
            {
                // Update MidiFile record - substruct one copy
                foreach (MidiFile file in files)
                {
                    file.CopiesLeft -= 1;
                    crumbsRepository.InsertOrUpdateMidiFile(file);
                }
            }

            // Update Invoice with SentMail ID
            invoice.Comment = userEmail + " | " + DateTime.Now.ToString("s");
            invoice.UserID = user.UserID;
            crumbsRepository.InsertOrUpdateInvoice(invoice);

            crumbsRepository.Save();
            return true;
        }
예제 #3
0
        public bool SendMail(User user, Mailing mail, Price price, MidiFile file)
        {
            if (user.Email == null)
                return false;

            if (price != null)
            {
                if (UpdatePromotionalPricing(user, price) == false)
                    return false;
            }

            try
            {
                SmtpClient client = new SmtpClient("m02.internetmailserver.net", 587);
                client.EnableSsl = false;
                MailAddress from = new MailAddress("*****@*****.**", "PianoCrumbs.com");
                MailAddress to = new MailAddress(user.Email, "");
                MailMessage message = new MailMessage(from, to);
                message.Subject = mail.Subject;
                message.IsBodyHtml = true;

                string fileName = "";
                string urlAction = "";

                if (file != null)
                {
                    fileName = file.Name;
                    urlAction = Url.Action("MidiView", "Crumbs", new { id = file.MidiID, name = file.DisplayName }, "http");
                }

                string latestLinks = "";
                IQueryable<MidiFile> latestFiles = crumbsRepository.LatestCrumbs.Take(5);
                foreach (var item in latestFiles)
                {
                    string url = Url.Action("MidiView", "Crumbs", new { id = item.MidiID, name = item.DisplayName }, "http");
                    latestLinks += string.Format("<p><a href='{0}'>{0}</a></p>", url);
                }

                string popualrLinks = "";
                IQueryable<MidiFile> popularFiles = crumbsRepository.RatedCrumbs.Take(5);
                foreach (var item in popularFiles)
                {
                    string url = Url.Action("MidiView", "Crumbs", new { id = item.MidiID, name = item.DisplayName }, "http");
                    popualrLinks += string.Format("<p><a href='{0}'>{0}</a></p>", url);
                }

                message.Body = string.Format(mail.Text, user.UserName, fileName, urlAction, latestLinks, popualrLinks);

                string unsubLink = Url.Action("Unsubscribe", "Account", new { username = user.UserName, reset = CrumbsExtensions.HashResetParams(user.UserName, user.UserKey) }, Request.Url.Scheme);
                message.Body += "<p><a href='" + unsubLink + "'>Unsubscribe</a>";

                NetworkCredential myCreds = new NetworkCredential("*****@*****.**", "Crumbs123", "");
                client.Credentials = myCreds;

                client.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception is:" + ex.ToString());
                return false;
            }

            SentMail sent = new SentMail();
            sent.UserID = user.UserID;
            sent.MailID = mail.ID;
            sent.Date = DateTime.Now;

            crumbsRepository.InsertOrUpdateSentMail(sent);
            crumbsRepository.Save();

            return true;
        }