public int EmailReAnimal(int id, string path, string address)
        {
            var animal = context.Animals.Find(id);

            if (animal != null)
            {
                var litter = context.Litters.Find(animal.LitterId);
                if (litter != null)
                {
                    var user = context.Users.Find(litter.UserId);
                    if (user != null)
                    {
                        var email = new Model.Emails();
                        email.UserId  = user.Id;
                        email.To      = address;
                        email.From    = user.Email;
                        email.Message = "Hello there!\n\n" +
                                        "Thank you for your interest in https://boopnz.azurewebsites.net/" + path + "/" + litter.Id + (litter.IsIndividual.Value ? "" : "/" + animal.Id) + "\n\n" +
                                        "We'll get in contact shortly to arrange a time for you to view them.\n\n" +
                                        "Thanks!\n\n" + user.Name;
                        string subject = "Interested in " + (animal.IsFemale.Value ? "Female" : "Male") + " " + litter.Breed + " " + litter.Animal;
                        SendEmailMessage(email, subject);
                    }
                }
            }
            return(id);
        }
        public int SendEmail()
        {
            string json = new StreamReader(Request.Body).ReadToEnd();

            Model.Emails email = JsonConvert.DeserializeObject <Model.Emails>(json);
            return(SendEmailMessage(email));
        }
        public int HoldAnimal(int id, string path, string address)
        {
            var animal = context.Animals.Find(id);

            if (animal != null)
            {
                var litter = context.Litters.Find(animal.LitterId);
                if (litter != null)
                {
                    var user = context.Users.Find(litter.UserId);
                    if (user != null)
                    {
                        var email = new Model.Emails();
                        email.UserId  = user.Id;
                        email.To      = address;
                        email.From    = user.Email;
                        email.Message = "Hello there!\n\n" +
                                        "Thank you for your interest in purchasing https://boopnz.azurewebsites.net/" + path + "/" + litter.Id + (litter.IsIndividual.Value ? "" : "/" + animal.Id) + "\n\n" +
                                        "Please make a payment of $" + litter.Deposit.ToString("F2") + " " +
                                        "into my bank account (" + user.BankAccount + ") within 24 hours to hold them for you.\n\n" +
                                        "Please include the reference number (" + litter.Id + "/" + animal.Id +
                                        ") in your deposit details to ensure we match up your payment correctly.\n\n" +
                                        "Thanks!\n\n" + user.Name;
                        string subject = "Deposit for " + (animal.IsFemale.Value ? "Female" : "Male") + " " + litter.Breed + " " + litter.Animal;
                        SendEmailMessage(email, subject);

                        animal.Hold = true;
                        context.SaveChanges();
                    }
                }
            }
            return(id);
        }
        public int SendEmailMessage(Model.Emails email, string subject = null)
        {
            var record = new Model.Emails();

            if (email.UserId > 0)
            {
                context.Emails.Add(record);
                record.UserId  = email.UserId;
                record.To      = email.To;
                record.From    = email.From;
                record.Message = email.Message;
                context.SaveChanges();
            }
#if DEBUG
            var env = "appsettings.Development.json";
#else
            var env = "appsettings.json";
#endif
            var config   = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(env).Build();
            var username = config.GetValue <string>("Smtp:Username");
            var password = config.GetValue <string>("Smtp:Password");

            if (username != null && password != null)
            {
                MailMessage msg = new MailMessage
                {
                    From    = new MailAddress(email.From),
                    Subject = subject != null ? subject : "New message from Boop"
                };
                msg.To.Add(new MailAddress(email.To));
                msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(email.Message, null, MediaTypeNames.Text.Plain));
                if (subject != null) // Need to cc the seller for deposit emails
                {
                    msg.CC.Add(new MailAddress(email.From));
                }

                SmtpClient client = new SmtpClient("smtp.sendgrid.net", System.Convert.ToInt32(587));
                client.Credentials = new NetworkCredential(username, password);
                client.Send(msg);
            }
            return(record.Id);
        }