public static void SendMail(List <string> toMails, string subject, string body, bool isHtmlFormat, SystemEmailSettingsWrapper mailSetting, MailType mailType) { SmtpClient objSmtpClient = mailSetting.GetSmtpClientByMailSetting(mailSetting); MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress(mailSetting.FromEmail, mailSetting.FromName); foreach (string toMail in toMails) { if (!string.IsNullOrEmpty(toMail)) { mailMessage.To.Add(new MailAddress(toMail)); } } mailMessage.Subject = subject; mailMessage.Body = body; mailMessage.IsBodyHtml = isHtmlFormat; mailMessage.Priority = MailPriority.Normal; mailMessage.BodyEncoding = System.Text.Encoding.UTF8; switch (mailType) { case MailType.SendSync: try { objSmtpClient.Send(mailMessage); SystemEmailQueueWrapper.SaveSuccessMail(mailMessage); } catch (Exception ex) { SystemEmailQueueWrapper.SaveErrorMail(mailMessage, ex); throw; } break; case MailType.SendAsync: objSmtpClient.SendCompleted += new SendCompletedEventHandler(objSmtpClient_SendCompleted); objSmtpClient.SendAsync(mailMessage, mailMessage); break; case MailType.SendAsyncSysDb: break; case MailType.SendSqlMail: break; default: objSmtpClient.Send(mailMessage); break; } }
private static void objSmtpClient_SendCompleted(object sender, AsyncCompletedEventArgs e) { MailMessage mailMessage = e.UserState as MailMessage; if (mailMessage != null) { if (e.Error != null) { SystemEmailQueueWrapper.SaveErrorMail(mailMessage, e.Error); } else { SystemEmailQueueWrapper.SaveSuccessMail(mailMessage); } } }