public void SendInputFileEmail(Application info, Int32 runNumberID, List <string> emailList) { try { SingletonLogger.Instance.Debug("Fetching email template for TOKEN = " + "DOWNLOAD_FILE"); EmailTemplate template = _emailTemplateRepository.EmailTemplate(x => x.ClientId == info.ClientId && x.ApplicationId == info.ApplicationId && x.EmailToken == "{{DOWNLOAD_FILE}}"); if (template != null) { SingletonLogger.Instance.Debug("templateID = " + template.EmailTemplateId + " runId " + runNumberID); EmailTracking tracking = new EmailTracking() { RunNumberId = runNumberID, FromEmailId = template.EmailFromSmtpId.ToString(), EmailTemplateId = template.EmailTemplateId, Subjects = template.Subject, Body = template.Body, EmailStatus = (int)EmailStatusType.Ready, EmailToIds = template.EmailToIds, EmailCcIds = template.EmailCcIds }; SingletonLogger.Instance.Debug("trackingID = " + tracking.EmailTrackingId); if (tracking != null) { tracking.SentDate = DateTime.Now; tracking.CreatedAt = DateTimeOffset.Now; tracking.Body = tracking.Body.Replace("{{FILE_LIST}}", CreateTable(emailList)); tracking.Body = tracking.Body.Replace("{{FILE_NAME}}", CreateTable(emailList)); tracking.Subjects = tracking.Subjects.Replace("{{APPLICATION_NAME}}", info.Name); tracking.Body = tracking.Body.Replace("{{APPLICATION_NAME}}", info.Name); tracking.Status = true; if (_emailTrackingRepository.Save(tracking) != 0) { SingletonLogger.Instance.Error("Error occured while email saving to email tracking."); } } } else { SingletonLogger.Instance.Debug("Email Template not found in database for TOKEN = " + "DOWNLOAD_FILE"); } } catch (Exception ex) { SingletonLogger.Instance.Error("Error occured while sending email about file download = " + ex.Message + " " + ex.InnerException != null?ex.InnerException.Message:"" + ex.ToString() + " " + ex.StackTrace); } }
public void SendInputFileEmail(int runComponentId, string component, string runNumber, string token, string message = "Success") { try { SingletonLogger.Instance.Debug("Fetching email template for TOKEN = " + token); Application appInfo = _applicationRepository.Find(_runNumberRepository.GetApplicationIdByRunNumber(runNumber)); int componentId = _runComponentRepository.Find(runComponentId).ComponentId; EmailTemplate template = _emailTemplateRepository.EmailTemplate(x => x.ClientId == appInfo.ClientId && x.ApplicationId == appInfo.ApplicationId && x.ApplicationComponentId == componentId && x.EmailToken == token); if (template != null) { EmailTracking tracking = new EmailTracking() { RunNumberId = _runNumberRepository.GetRunNumberIdByRunNumber(runNumber), FromEmailId = template.EmailFromSmtpId.ToString(), EmailTemplateId = template.EmailTemplateId, Subjects = template.Subject, Body = template.Body, EmailStatus = (int)EmailStatusType.Ready }; if (tracking != null) { tracking.SentDate = DateTime.Now; tracking.Body = tracking.Body.Replace("{{CLIENT_NAME}}", appInfo.ClientId.ToString()); tracking.Body = tracking.Body.Replace("{{APPLICATION_NAME}}", appInfo.Name).Replace("{{COMPONENT_NAME}}", component); tracking.Body = tracking.Body.Replace("{{MESSAGE}}", message); tracking.Subjects = tracking.Subjects.Replace("{{COMPONENT_NAME}}", component).Replace("{{APPLICATION_NAME}}", appInfo.Name).Replace("{{CLIENT_NAME}}", appInfo.ClientId.ToString()); if (_emailTrackingRepository.Save(tracking) != 0) { SingletonLogger.Instance.Error("Error occured while email saving to email tracking."); } } } else { SingletonLogger.Instance.Debug("Email Template not found in database for TOKEN = " + token); } } catch (Exception ex) { SingletonLogger.Instance.Error("Error occured while sending email about file download = " + ex.Message); } }
public void SendEmails() { try { var emailDetail = emailTrackingRepository.FindAll().Where(x => x.EmailStatus.Equals((int)EmailStatusType.Ready)).OrderBy(x => x.CreatedAt).Take(10).ToList(); SingletonLogger.Instance.Debug("Ready Emails are :" + emailDetail.Count()); smtpDetail = smtpDetailsRepository.Find(1);//TODO static value replace later SingletonLogger.Instance.Debug("Email Send From " + smtpDetail.SmtpUser); smtpMail = new SmtpMail(smtpDetail.SmtpHost, smtpDetail.SmtpUser, smtpDetail.Password); foreach (var email in emailDetail) { RunDetail runDetail = runDetailsRepository.Find(email.RunNumberId); SingletonLogger.Instance.Debug("Email Id = " + email.EmailTrackingId); if (string.IsNullOrEmpty(email.EmailToIds)) { SingletonLogger.Instance.Debug("To email Ids not found in DB."); continue; } SingletonLogger.Instance.Debug("Email Sending To " + email.EmailToIds); smtpMail.To = email.EmailToIds; smtpMail.From = smtpDetail.SmtpUser; smtpMail.Subject = email.Subjects.Replace("{{RUN_NUMBER}}", runDetail.RunNumber); smtpMail.Body = email.Body.Replace("{{RUN_NUMBER}}", runDetail.RunNumber); if (!string.IsNullOrEmpty(email.EmailCcIds)) { smtpMail.Cc = email.EmailCcIds; } try { SingletonLogger.Instance.Debug("Start Sending Email"); var result = smtpMail.SendEmail(); if (string.IsNullOrEmpty(result)) { email.EmailStatus = (int)EmailStatusType.Success; SingletonLogger.Instance.Debug("Email Status Set to SUCCESS"); email.SentMessage = "Email sent successfully."; SingletonLogger.Instance.Debug("Email sent successfully"); } else { SingletonLogger.Instance.Error("Error in sending email. Detail : " + result); email.EmailStatus = (int)EmailStatusType.Error; email.SentMessage = result; } } catch (Exception ex) { SingletonLogger.Instance.Error("Error in sending email. Detail : " + ex.ToString()); email.EmailStatus = (int)EmailStatusType.Error; email.SentMessage = ex.Message; } //update entry SingletonLogger.Instance.Debug("Updating Status in Email Tracking table with Id = " + email.EmailTrackingId); email.Status = true; email.SentDate = DateTime.Now; emailTrackingRepository.Save(email); SingletonLogger.Instance.Debug("Updated Status " + email.Status + " in Email Tracking table successfully"); } SingletonLogger.Instance.Debug("Email component Complete"); } catch (Exception ex) { SingletonLogger.Instance.Error(ex.ToString()); } }