public MailController(IEmail emailService)
 {
     #region Write Log
     var dtEntry = DateTime.Now;
     var address = baseAddress + ".MailController(IEmail emailService)";
     var dic     = LogBusiness.GetDictionary();
     dic.Add(LogFieldName.FullyQualifiedFunctionName, address);
     var dicParams = LogBusiness.GetDictionary();
     dicParams.Add(LogFieldName.Token, dicParams);
     LogBusiness.CustomLog("AnonymousUser", LogEvent.MethodStart, dic, dicParams);
     #endregion
     try
     {
         _emailService = emailService;
     }
     catch (Exception ex)
     {
         #region Write Log
         dic = LogBusiness.GetDictionary();
         dic.Add(LogFieldName.FullyQualifiedFunctionName, baseAddress);
         dic.Add(LogFieldName.ErrorMessage, ex.Message);
         dic.Add(LogFieldName.StackTrace, ex.StackTrace);
         dic.Add(LogFieldName.TimeElapsed, ((int)(DateTime.Now - dtEntry).TotalMilliseconds).ToString());
         LogBusiness.CustomLog("EMail informations could not be loaded to MailAPI possibly from unexpected error", LogEvent.ErrorEvent, dic, dicParams);
         #endregion
     }
 }
 public EmailWindowsService()
 {
     InitializeComponent();
     #region Write Log
     DateTime     dtEntry = DateTime.Now;
     const string address = BaseAddress + ".MailController(IEmail emailService)";
     var          dic     = LogBusiness.GetDictionary();
     dic.Add(LogFieldName.FullyQualifiedFunctionName, address);
     var dicParams = LogBusiness.GetDictionary();
     dicParams.Add(LogFieldName.Token, dicParams);
     dic.Add(LogFieldName.TimeElapsed, ((int)(DateTime.Now - dtEntry).TotalMilliseconds).ToString());
     LogBusiness.CustomLog("AnonymousUser", LogEvent.MethodStart, dic, dicParams);
     _cancellationToken = _cts.Token;
     #endregion
 }
        private async Task WorkerAsync()
        {
            _timer.Stop();

            string fromMail = "";

            try
            {
                _emails = EmailService.GetAll().Where(x => !x.IsSent).OrderBy(x => x.Id).ToList();

                if (_emails.Count == 0)
                {
                    _timer.Start(); return;
                }

                foreach (var email in _emails)
                {
                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri(_apiUrl);

                        using (var stream = new MemoryStream())
                            using (var bson = new BsonWriter(stream))
                            {
                                var jsonSerializer = new JsonSerializer();

                                fromMail = email.From;

                                _emailAttachments = EmailAttachmentService.GetAllNoTracking().Where(x => x.EmailId == email.Id).ToList();

                                var attachments = _emailAttachments.Select(attachment => new EMailAttachmentViewModel
                                {
                                    File     = attachment.File,
                                    FileName = attachment.FileName,
                                    EmailId  = attachment.EmailId
                                }).ToList();

                                if (email.Retry >= _retryLimitation)
                                {
                                    continue;
                                }
                                var willBeSentAgainEmail = new EMailViewModel
                                {
                                    Id               = email.Id,
                                    From             = email.From,
                                    To               = email.To,
                                    Subject          = email.Subject,
                                    Body             = email.Body,
                                    Bcc              = email.Bcc,
                                    Cc               = email.Cc,
                                    Exception        = email.Exception,
                                    Retry            = ++email.Retry,
                                    SmtpServer       = email.SmtpServer,
                                    IsRead           = false,
                                    IsSent           = false,
                                    LastTryDate      = DateTime.Now,
                                    EmailAttachments = attachments
                                };

                                jsonSerializer.Serialize(bson, willBeSentAgainEmail);

                                client.DefaultRequestHeaders.Accept.Clear();
                                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/bson"));

                                var byteArrayContent = new ByteArrayContent(stream.ToArray());
                                byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/bson");

                                await client?.PostAsync("api/mail/sendemailasync", byteArrayContent, _cancellationToken);
                            }
                    }
                }
                _timer.Start();
            }
            catch (Exception ex)
            {
                _timer.Start();
                #region Write Log
                string address = BaseAddress + ".MailController(IEmail emailService)";
                var    dic     = LogBusiness.GetDictionary();
                dic.Add(LogFieldName.FullyQualifiedFunctionName, address);
                var dicParams = LogBusiness.GetDictionary();
                dicParams.Add(LogFieldName.Token, dicParams);
                dic = LogBusiness.GetDictionary();
                dic.Add(LogFieldName.FullyQualifiedFunctionName, address);
                dic.Add(LogFieldName.ErrorMessage, ex.Message);
                dic.Add(LogFieldName.StackTrace, ex.StackTrace);
                LogBusiness.CustomLog(fromMail, LogEvent.ErrorEvent, dic, dicParams);
                #endregion
            }
            _timer.Start();
        }
        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
                            }
                        }
                    }
                }
            });
        }