public async void SendEMailAsync([FromBody] EMailModel model) { await Task.Run(() => { if (model == null) { return; } #region Write Log var dtEntry = DateTime.Now; var address = baseAddress + ".SendEMail([FromBody]EMailModel model)"; var dic = LogBusiness.GetDictionary(); dic.Add(LogFieldName.FullyQualifiedFunctionName, address); var dicParams = LogBusiness.GetDictionary(); dicParams.Add(LogFieldName.Token, dicParams); LogBusiness.CustomLog(model.From, LogEvent.MethodStart, dic, dicParams); #endregion start: using (var smtp = new SmtpClient(MailSmtpHost)) { using (var message = new MailMessage()) { smtp.Credentials = new NetworkCredential(); smtp.Host = MailSmtpHost; smtp.Port = MailSmtpPort; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.UseDefaultCredentials = false; message.IsBodyHtml = true; if (model.Retry == 0) { try { if (File.Exists(@"/Resources/yourMailPic.gif")) { message.AlternateViews.Add(CreateEmbeddedImage("/Resources/yourMailPic.gif")); } if (!string.IsNullOrEmpty(model.From)) { var fromList = model.From.Split('&'); message.From = new MailAddress(fromList[0], fromList[1]); } if (!string.IsNullOrEmpty(model.Subject)) { message.Subject = model.Subject; } if (!string.IsNullOrEmpty(model.Body)) { message.Body = model.Body; } if (!string.IsNullOrEmpty(model.To)) { var toList = model.To.Split(';'); foreach (var toRaw in toList) { var toRawList = toRaw.Split('&'); var addr = new MailAddress(toRawList[0], toRawList[1]); message.To.Add(addr); } } //CC's if (!string.IsNullOrEmpty(model.Cc)) { var ccMails = model.Cc.Split(';').ToArray(); foreach (var ccEmail in ccMails) { var ccRawList = ccEmail.Split('&'); var ccaddr = new MailAddress(ccRawList[0], ccRawList[1]); message.CC.Add(ccaddr); } } //BCC's if (!string.IsNullOrEmpty(model.Bcc)) { var bccMails = model.Bcc.Split(';').ToArray(); foreach (var bccEmail in bccMails) { var bccRawList = bccEmail.Split('&'); var bccaddr = new MailAddress(bccRawList[0], bccRawList[1]); message.Bcc.Add(bccaddr); } } //Attachment's if (model.EmailAttachments.Count > 0) { foreach (var attachment in model.EmailAttachments) { var attachmentMetaData = new Attachment(new MemoryStream(attachment.File), attachment.FileName, MediaTypeNames.Application.Octet); message.Attachments.Add(attachmentMetaData); } } model.SentDate = DateTime.Now; var mail = new Email { Subject = model.Subject, Bcc = model.Bcc, Cc = model.Cc, Body = model.Body, From = model.From, To = model.To, SmtpServer = MailSmtpHost }; foreach (var attachment in model.EmailAttachments) { var attach = new EmailAttachment { File = attachment.File, FileName = attachment.FileName }; mail.EmailAttachments.Add(attach); } _emailService.Add(mail); _emailService.Save(); var savePkId = mail.Id; var updateEmail = _emailService.GetAll().FirstOrDefault(x => x.Id == savePkId); try { smtp.Send(message); if (updateEmail == null) { return; } updateEmail.IsSent = true; updateEmail.SentDate = DateTime.Now; _emailService.Update(updateEmail); _emailService.Save(); } catch (Exception e) { if (updateEmail != null) { updateEmail.Exception = e.InnerException == null ? e.Message : e.Message + " --> " + e.InnerException.Message; updateEmail.LastTryDate = DateTime.Now; updateEmail.Retry = updateEmail.Retry++; _emailService.Update(updateEmail); _emailService.Save(); } } } catch (Exception ex) { #region Write Log dic = LogBusiness.GetDictionary(); dic.Add(LogFieldName.FullyQualifiedFunctionName, address); dic.Add(LogFieldName.ErrorMessage, ex.Message); dic.Add(LogFieldName.StackTrace, ex.StackTrace); LogBusiness.CustomLog(model.From, LogEvent.ErrorEvent, dic, dicParams); MailSmtpHost = MailSmtpHost2; goto start; #endregion } finally { #region Write Log dic = LogBusiness.GetDictionary(); dic.Add(LogFieldName.FullyQualifiedFunctionName, address); dic.Add(LogFieldName.TimeElapsed, ((int)(DateTime.Now - dtEntry).TotalMilliseconds).ToString()); LogBusiness.CustomLog(model.From, LogEvent.MethodEnd, dic, dicParams); #endregion } } else { try { if (!string.IsNullOrEmpty(model.From)) { var fromList = model.From.Split('&'); message.From = new MailAddress(fromList[0], fromList[1]); } if (!string.IsNullOrEmpty(model.Subject)) { message.Subject = model.Subject; } if (!string.IsNullOrEmpty(model.Body)) { message.Body = model.Body; } if (!string.IsNullOrEmpty(model.To)) { var toList = model.To.Split(';'); foreach (var toRaw in toList) { var toRawList = toRaw.Split('&'); var addr = new MailAddress(toRawList[0], toRawList[1]); message.To.Add(addr); } } //CC's if (!string.IsNullOrEmpty(model.Cc)) { var ccMails = model.Cc.Split(';').ToArray(); foreach (var ccEmail in ccMails) { var ccRawList = ccEmail.Split('&'); var ccaddr = new MailAddress(ccRawList[0], ccRawList[1]); message.CC.Add(ccaddr); } } //BCC's if (!string.IsNullOrEmpty(model.Bcc)) { var bccMails = model.Bcc.Split(';').ToArray(); foreach (var bccEmail in bccMails) { var bccRawList = bccEmail.Split('&'); var bccaddr = new MailAddress(bccRawList[0], bccRawList[1]); message.Bcc.Add(bccaddr); } } if (model.EmailAttachments.Count > 0) { foreach (var attachment in model.EmailAttachments) { var attachmentMetaData = new Attachment(new MemoryStream(attachment.File), attachment.FileName, MediaTypeNames.Application.Octet); message.Attachments.Add(attachmentMetaData); } } var updateEmail = _emailService.GetAll().FirstOrDefault(x => x.Id == model.Id); start2: try { smtp.Send(message); if (updateEmail == null) { return; } updateEmail.SmtpServer = smtp.Host; updateEmail.Retry = model.Retry; // WindowsSerive deneme sayısını artırıyor. updateEmail.IsSent = true; updateEmail.SentDate = DateTime.Now; updateEmail.Exception = "Last Exception was :" + model.Exception; updateEmail.LastTryDate = model.LastTryDate; _emailService.Update(updateEmail); _emailService.Save(); } catch (Exception e) { if (updateEmail != null) { updateEmail.SmtpServer = smtp.Host; updateEmail.Exception = e.InnerException == null ? e.Message : e.Message + " --> " + e.InnerException.Message; updateEmail.LastTryDate = DateTime.Now; updateEmail.Retry = model.Retry; _emailService.Update(updateEmail); _emailService.Save(); } smtp.Host = MailSmtpHost2; goto start2; } } catch (Exception ex) { #region Write Log dic = LogBusiness.GetDictionary(); dic.Add(LogFieldName.FullyQualifiedFunctionName, address); dic.Add(LogFieldName.ErrorMessage, ex.Message); dic.Add(LogFieldName.StackTrace, ex.StackTrace); LogBusiness.CustomLog(model.From, LogEvent.ErrorEvent, dic, dicParams); #endregion } finally { #region Write Log dic = LogBusiness.GetDictionary(); dic.Add(LogFieldName.FullyQualifiedFunctionName, address); dic.Add(LogFieldName.TimeElapsed, ((int)(DateTime.Now - dtEntry).TotalMilliseconds).ToString()); LogBusiness.CustomLog(model.From, LogEvent.MethodEnd, dic, dicParams); #endregion } } } } }); }