using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Mail; using System.Text; using System.Threading.Tasks; using TMTCronJob.Model; namespace TMTCronJob.Services { public class MailService { public void SendEmail(List problemInfridgments, List admins) { //use the problemInfridgments and send emails to the admins StringBuilder sb = new StringBuilder(); sb.AppendLine("The Cronjob could not complete the following payments: "); //---> need a proper format here. //can make the email look better foreach (var problemInfridgment in problemInfridgments) { sb.AppendLine("infringementID: " + problemInfridgment.InfringementID.ToString() + " NoticeNumber: " + problemInfridgment.NoticeNumber); } try { SmtpClient client = new SmtpClient() { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential() { UserName = "sherewalloyd@gmail.com", Password = "xxxxxx" } }; MailAddress from = new MailAddress("sherewalloyd@gmail.com", "Lloyd"); MailAddress to = new MailAddress("sherewalloyd@gmail.com"); MailMessage message = new MailMessage(); message.From = from; message.To.Add(to); foreach (var adminEmail in admins) { message.CC.Add(new MailAddress(adminEmail)); } message.Subject = "PMF Error Payments"; message.Body = sb.ToString(); client.Send(message); } catch (Exception ex) { throw ex; } } } }