//------------------- email ------------------ public static bool SendMail(CustomMailFormat mf) { bool ret = false; var message = new MailMessage(); if (mf.ToAddresses.Count() > 0) { foreach (var to in mf.ToAddresses) { message.To.Add(new MailAddress(to)); } } else { message.To.Add(new MailAddress(mf.To)); } // replace with valid value message.From = new MailAddress(mf.From); // replace with valid value message.Subject = mf.Subject; message.Body = mf.Message; message.IsBodyHtml = true; using (var smtp = new SmtpClient()) { var credential = new NetworkCredential { UserName = SMTPConfig.UserName, // replace with valid value Password = SMTPConfig.Password // replace with valid value }; smtp.Credentials = credential; smtp.Host = SMTPConfig.Host; smtp.Port = SMTPConfig.Port; smtp.EnableSsl = false; try { smtp.Send(message); } catch { return(false); } ret = true; } return(ret); }
public static bool SendMailByRole(List <string> roles, string subjectTag, string groupMessage) { bool ret = false; foreach (var role in roles) { CustomMailFormat mf = new CustomMailFormat(); List <string> toAddresses = GetMailAddressesByRole(role); mf.Subject = "FAV [" + subjectTag + "]"; mf.To = toAddresses.First(); mf.ToAddresses = toAddresses; mf.Message = groupMessage; mf.From = MailConfig.NotificationMailAddress; if (toAddresses.Count > 0) { SendMail(mf); } } return(ret); }