public static async Task addEmailToCache(EmailCacheModel ECM) { using (WebDocs.DataAccessLayer.WebDocsEntities db = new DataAccessLayer.WebDocsEntities()) { ECM.EntityState = DomainModels.EntityState.Added; db.EmailCacheModels.Add(ECM); await db.SaveChangesAsync(); } }
private void SendEmail(EmailCacheModel EC, EmailSetting ES) { Boolean SentSuccsessfully; try { Common.Helper.Email.EmailHelper.sendMessage( _ToAddress: EC.EmailRecipient.Email, _FromAddress: EC.EmailSender.Email, _ToName: EC.EmailRecipient.UserFullName, _FromName: EC.EmailSender.UserFullName, _Subject: EC.EmailSubject, _Message: EC.EmailMessage, _SMTP_HOST: ES.SmtpHost, _SMTP_PORT: ES.SmtpPort, _IsSsl: ES.SslEnabled, _Credentials_Password: ES.Password, _Credentials_UserName: ES.UserName ); SentSuccsessfully = true; //Common.WindowsServices.ServiceLogFiles.WrtieToLog("11111111111111111111Successfully Sent Email for: " + EC.EmailRecipient.UserFullName + " From: " + EC.EmailSender.UserFullName + " - email Cahce Item: " + EC.EmailCacheID); } catch { SentSuccsessfully = false; // Common.WindowsServices.ServiceLogFiles.WrtieToLog("11111111111111111111111111111Failed Sent Email for: " + EC.EmailRecipient.UserFullName + " From: " + EC.EmailSender.UserFullName + " - email Cahce Item: " + EC.EmailCacheID); } using (WebDocs.DataAccessLayer.WebDocsEntities db = new DataAccessLayer.WebDocsEntities()) { if (SentSuccsessfully) { EC.HasBeenSent = true; db.EmailCacheModels.Attach(EC); db.Entry(EC).State = EntityState.Modified; db.SaveChanges(); //Common.WindowsServices.ServiceLogFiles.WrtieToLog("Successfully Updated Email for: " + EC.EmailRecipient.UserFullName + " From: " + EC.EmailSender.UserFullName + " - email Cahce Item: " + EC.EmailCacheID); } else { if (EC.RetryAttempt < 5) { EC.RetryAttempt = EC.RetryAttempt + 1; db.EmailCacheModels.Attach(EC); db.Entry(EC).State = EntityState.Modified; db.SaveChanges(); //Common.WindowsServices.ServiceLogFiles.WrtieToLog("222222222222222222222222Failed to Update Email for: " + EC.EmailRecipient.UserFullName + " From: " + EC.EmailSender.UserFullName + " - email Cahce Item: " + EC.EmailCacheID); } } } }
private async void EmailTimer_Tick(object sender, ElapsedEventArgs e) { //Stop timer untill the function completes. EmailTimer.Enabled = false; //Common.WindowsServices.ServiceLogFiles.WrtieToLog("Timer tick event entered and stoped - 1"); //get relivant emails and email settings. try { using (WebDocs.DataAccessLayer.WebDocsEntities db = new DataAccessLayer.WebDocsEntities()) { ES = db.EmailSettings.FirstOrDefault <EmailSetting>(); EmailToSend = await db.EmailCacheModels.Where(a => a.HasBeenSent == false) .Include(a => a.EmailSender) .Include(a => a.EmailRecipient) .ToListAsync <EmailCacheModel>(); } //Common.WindowsServices.ServiceLogFiles.WrtieToLog("Email Settings and List of unsent emails are populated - 2"); } catch { //Common.WindowsServices.ServiceLogFiles.WrtieToLog("Failed at - 2 Error Message: " + ex.Message + " Inner Message : " + ex.InnerException.Message); } //if any emails are avaiable send emails. //Common.WindowsServices.ServiceLogFiles.WrtieToLog("Sending unsent emails START - 3"); foreach (EmailCacheModel EC in EmailToSend) { SendEmail(EC, ES); //Common.WindowsServices.ServiceLogFiles.WrtieToLog("Email sent To: " + EC.EmailRecipient.UserFullName + " - " + EC.EmailRecipient.Email +" - 3"); } //restart Timer //Common.WindowsServices.ServiceLogFiles.WrtieToLog("Email Timer Resstarted - 4"); this.EmailTimer.Enabled = true; }