コード例 #1
0
        public async Task <ActionResult> ResidentAnimal(string animal)
        {
            if ((animal.Length > 4) && (animal.Substring(0, 4).Equals("send")))
            {
                // tutaj cała hochsztaplerka żeby wydobyć z bazy odpowiednie zwirze po ID
                int             id  = Int32.Parse(animal.Substring(4, animal.Length - 4));
                List <Resident> l   = _residentRepository.GetResidentsInfos();
                Resident        res = l.Where <Resident>(x => x.Id == id).Single <Resident>();

                string password = "******";

                SmtpClient smtpClient = new SmtpClient()
                {
                    Credentials = new System.Net.NetworkCredential("*****@*****.**", password),
                    EnableSsl   = true,
                    Port        = 587,
                    Host        = "smtp.gmail.com"
                };

                MailMessage mail = new MailMessage
                {
                    From         = new MailAddress("*****@*****.**", "Animal Shelter", System.Text.Encoding.UTF8),
                    Subject      = "Animal Shelter: Your " + res.Type.ToString() + " is ready to come back home",
                    BodyEncoding = System.Text.Encoding.UTF8
                };

                mail.Bcc.Add(res.OwnerEmail.ToString());

                StringBuilder body = new StringBuilder();

                body.AppendLine("<html>");
                body.AppendLine("<head>");
                body.AppendLine("</head>");
                body.AppendLine("<body>");
                body.AppendLine("<h1>Your " + res.Type.ToString() + ", " + res.Name.ToString() + " is ready to come back home</h1>");
                body.AppendLine("<h3>" + res.Name.ToString() + " was staying here from: " + res.From.ToString() + " to: " + res.To.ToString() + "</h3>");
                body.AppendLine("</body>");
                body.AppendLine("</html>");

                mail.Body       = body.ToString();
                mail.IsBodyHtml = true;

                try
                {
                    await smtpClient.SendMailAsync(mail);
                }
                catch (Exception ex)
                {
                    ViewBag.Error = ex.ToString();
                }
            }
            else
            {
                Resident resident = new Resident {
                    Id = Int32.Parse(animal)
                };
                await _residentRepository.DeleteResidentAsync(resident);
            }

            return(Redirect("/Hotel"));
        }