public static void SendMailInner(string username, string subj, string content)
        {
            UserDataContext db = new UserDataContext();

            var EmailAddress = from user in db.DBUserData
                               where user.Username == username
                               select user.Email;

            string email = EmailAddress.ToList().First();
            string subject = subj;
            string emailFrom = "*****@*****.**";
            string password = "******";
            MailMessage Mail = new MailMessage(emailFrom, email);
            SmtpClient client = new SmtpClient();
            client.Port = 587;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Host = "smtp.gmail.com";
            client.Credentials = new NetworkCredential(emailFrom, password);
            client.EnableSsl = true;
            Mail.Subject = subject;
            Mail.To.Add(email);
            Mail.Body = content;
            client.Send(Mail);
        }
        /// <summary>
        /// Fills the checkout page with data from the user' cart.
        /// </summary>
        /// <returns>The modifed checkout view</returns>
        public ActionResult Checkout()
        {
            UserDataContext udc = new UserDataContext();
            PicturesContext pc = new PicturesContext();
            PictureModel pm = new PictureModel();

            var a = from user in udc.DBUserData
                    where user.Username == User.Identity.Name
                    select user.Order;
            char[] order = a.ToArray().First().ToArray();
            string tempstr = "";
            List<int> orderList = new List<int>();
            for (int i = 0; i < order.Count(); i++)
            {

                if (char.IsNumber(order[i]))
                {

                    string temp = Convert.ToString(order[i]);
                    tempstr += temp;
                }
                else
                {
                    if (tempstr.Length > 0)
                    {
                        orderList.Add(Convert.ToInt32(tempstr));
                        tempstr = "";
                    }

                }
                if (i + 1 == order.Count())
                {
                    orderList.Add(Convert.ToInt32(tempstr));
                }
            }
            List<PictureModel> photoList = new List<PictureModel>();
            foreach (int ID in orderList)
            {
                var photo = from x in pc.Picture
                            where x.Id == ID
                            select x;
                if(photo.Any())
                photoList.Add(photo.ToList().First());
            }
            ViewData["photoList"] = photoList;
            return View("Checkout");
        }
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    EmailAddressAttribute emailCheck = new EmailAddressAttribute();

                    if (emailCheck.IsValid(model.Email))
                    {
                        UserDataContext db = new UserDataContext();

                        CustSecurityController Secure = new CustSecurityController();
                        WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

                        UserData dataProfile = new UserData(model.UserName, model.Email, model.Street, model.HouseNumber, model.City, model.PostalCode, null);

                        db.DBUserData.Add(dataProfile);
                        db.SaveChanges();

                        Secure.Create(new IPProfile(model.UserName, Request.UserHostAddress));
                        WebSecurity.Login(model.UserName, model.Password);
                        return RedirectToAction("Overview", "Page");
                    }
                    else
                    {
                        ModelState.AddModelError("", "The email address entered is not valid");
                    }
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        /// <summary>
        /// Simply, logs the user off.
        /// </summary>
        /// <returns>""</returns>
        public ActionResult LogOff()
        {
            // Ingelogde gebruiker ophalen
            UserDataContext uContext = new UserDataContext();
            UserData user;
            string userName = User.Identity.Name;
            var a = from x in uContext.DBUserData
                    where x.Username == userName
                    select x;
            user = a.ToList().First();

            user.Order = null;

            try
            {
                uContext.SaveChanges();
            }
            catch (Exception e)
            {
                return Json("The system was unable to save your order");
            }

            Session["order"] = null;

            WebSecurity.Logout();

            return RedirectToAction("Overview", "Page");
        }
        /// <summary>
        ///  gets the list of users
        /// </summary>
        /// <param name="userName">user`s email</param>
        /// <returns>Mail</returns>
        private string getEmail(string userName)
        {
            string Email = "";
            UserDataContext db = new UserDataContext();
            var mail = from user in db.DBUserData
                       where user.Username == userName
                       select user.Email;
            try
            {
                string adress = mail.ToList().First();
                var addr = new System.Net.Mail.MailAddress(adress);
                Email = adress;
            }
            catch
            {
                Email = "*****@*****.**";
            }

            return Email;
        }
 /// <summary>
 /// Gets all users for select in sendto mail 
 /// </summary>
 /// <returns>mail page</returns>
 public ActionResult SendMail()
 {
     UserDataContext db = new UserDataContext();
     var username = from user in db.DBUserData select user.Username;
     ViewBag.username = username;
     return View("SendMail");
 }
        public ActionResult RemoveFromCart(FormCollection collection)
        {
            Session["order"] = collection["order"].ToString();

            // Get logged in user
            UserDataContext uContext = new UserDataContext();
            UserData user;
            string userName = User.Identity.Name;
            var a = from x in uContext.DBUserData
                    where x.Username == userName
                    select x;
            user = a.ToList().First();

            user.Order = Session["order"].ToString();

            try
            {
                uContext.SaveChanges();
            }
            catch (Exception e)
            {
                return Json("The system was unable to save your order");
            }

            return Json(Session["order"]);
        }
        public ActionResult PhotoToCart(FormCollection collection)
        {
            if (collection["id"].Contains(',') == true)
                collection["id"].Remove(0, 1);
            int id = Convert.ToInt16(collection["id"]);

            // Get photo
            PicturesContext pContext = new PicturesContext();
            PictureModel photo = pContext.Picture.Find(id);

            // Get logged in user
            UserDataContext uContext = new UserDataContext();
            UserData user;
            string userName = User.Identity.Name;
            var a = from x in uContext.DBUserData
                    where x.Username == userName
                    select x;
            if (a.Any())
            {
                user = a.ToList().First();
            }
            else
            {
                return Json("The system was unable to save your order"); ;
            }

            var order = Session["order"];
            if (order == null)
            {
                Session["order"] = id.ToString();
            }
            else
            {
                Session["order"] = order + ", " + id;
            }
            order = Session["order"];

            user.Order = order.ToString();

            try
            {
                uContext.SaveChanges();
            }
            catch (Exception e)
            {
                return Json("The system was unable to save your order");
            }

            return Json(photo);
        }
        /// <summary>
        /// Function to get the order of a costumer.
        /// </summary>
        /// <returns>The order</returns>
        public ActionResult GetOrder()
        {
            var order = "";

            // Get logged in user
            if (User.Identity.IsAuthenticated)
            {
                UserDataContext uContext = new UserDataContext();
                UserData user;
                string userName = User.Identity.Name;
                var a = from x in uContext.DBUserData
                        where x.Username == userName
                        select x;
                user = a.ToList().First();

                if (Session["order"] == null)
                {
                    order = user.Order;
                    Session["order"] = order;
                }
                else
                {
                    order = Session["order"].ToString();
                }
            }

            return Json(order);
        }