0

I need to send a list of tasks to users in my MVC application.

 public static void Send()
    {
        try
        {
            StringBuilder sb = new StringBuilder();

            UserRepository userRepo = new UserRepository();
            var users = userRepo.GetUsers();

            TaskRepository repo = new TaskRepository();

            foreach (var rec in users)
            {
                var tasks = repo.GetTasksOfUser(rec.UserID);

                sb.AppendLine("Task Name                              Due Date");
                sb.AppendLine("---------                              --------");

                foreach (var results in tasks)
                {
                    sb.AppendLine(results.TaskName.PadRight(30, ' ') + "          " + String.Format("{0:MM/dd/yyyy HH:mm tt}", results.DueDate));
                }

                dynamic email = new Email("TaskEmail");
                email.To = rec.Email;
                email.From = "no-reply@xyz.com";
                email.Subject = "Pending Tasks";
                email.Content = sb.ToString();
                email.Send();
            }
        }
        catch (Exception ex)
        {

        }
    }

TaskEmail.cshtml

 @{
    Layout = null;
}

To: @ViewBag.To
From: @ViewBag.From
Subject: @ViewBag.Subject

<h3>Pending Tasks</h3>

@ViewBag.Content

Currently I am doing like this. But its not formatted. What I need is to an object with the list of tasks to the email tempalate.

sony
  • 1,453
  • 3
  • 35
  • 79
  • 1
    How about you stick "tasks" in your email's properties (`email.Tasks = tasks;`) and then in the template use a foreach to loop through the list of tasks and print a HTML table with the task data in it? – ADyson Oct 05 '16 at 14:16
  • ADyson, that worked... thanks – sony Oct 05 '16 at 14:28
  • You should post the answer you came up with as the answer to this question. It would help others out that are having the same questions. :) – Jamie Dec 04 '18 at 21:15

0 Answers0