Пример #1
0
        public async Task <ActionResult> EventSend()
        {
            List <Subscriber> subscribers = await _subcriberRepository.GetSubscribersAsync();

            List <Event> events = await _eventRepository.GetEventsAsync();

            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 Events Buletin",
                BodyEncoding = System.Text.Encoding.UTF8
            };

            // dodajemy odbiorców z bazy
            foreach (var x in subscribers)
            {
                mail.Bcc.Add(x.Email);
            }

            StringBuilder body = new StringBuilder();

            body.AppendLine("<html>");
            body.AppendLine("<head>");
            body.AppendLine("<style>");
            body.AppendLine("th, td { padding: 10px; }");
            body.AppendLine("</style>");
            body.AppendLine("</head>");
            body.AppendLine("<body>");
            body.AppendLine("<h1>Upcoming events:</h1>");
            body.AppendLine("<table border=\"0\" style=\"border: none;\">");
            body.AppendLine("<tr><th> Name </th><th> Date </th><th> Time </th><th> About </th></tr>");

            foreach (var y in events)
            {
                body.Append("<tr><td>");
                body.Append(y.Name);
                body.Append("</td><td>");
                body.Append(y.Date);
                body.Append("</td><td>");
                body.Append(y.Time);
                body.Append("</td><td>");
                body.Append(y.About);
                body.Append("</td></tr>");
                body.AppendLine();
            }
            body.AppendLine("</table>");
            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();
            }

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