internal Checker(List <CheckService> checkServices, MailHost mail) { this.checklist = checkServices; this.smtp = null; if (mail != null) { smtp = new SmtpClient(mail.Host, mail.Port); smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential(mail.Username, mail.Password); } }
public static void Send(MailHost config, Email mail) { using (SmtpClient smtp = new SmtpClient(config.Host, config.Port)) using (MailMessage msg = new MailMessage()) { try { smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential(config.Username, config.Password); msg.From = new MailAddress(mail.From, mail.Name); foreach (string item in mail.To) { if (IsValidEmail(item)) { msg.To.Add(item); } } foreach (string item in mail.cc) { if (IsValidEmail(item)) { msg.CC.Add(item); } } msg.Subject = mail.Subject; msg.IsBodyHtml = mail.IsHtml; msg.Body = mail.Body; msg.BodyEncoding = Encoding.UTF8; smtp.Send(msg); } catch (Exception ex) { Trace.WriteLine("Fail to send mail"); Trace.WriteLine(ex.Message); throw; } } }