private void SendEmail(string toEmail, string subject, string message, EmailSenderOptions Options) { MailMessage mail = GetMailMessage(toEmail, subject, message, Options.emailSender, Options.emailUserName, Options.useHtml); SmtpClient client = GetSmtpClient(Options.emailServer, Options.emailPort, Options.requiresAuthentication, Options.emailEmail, Options.emailPassword, Options.useSsl); client.Send(mail); }
void IBatchTypes.OnExecute(dynamic obj) { VPC.Entities.BatchType.BatchType batchType = (VPC.Entities.BatchType.BatchType)obj[0]; var tenantId = Guid.Parse(batchType.TenantId.Value); try { ISettingManager _iSettingManager = new SettingManager(); IEntityResourceManager _iEntityResourceManager = new VPC.Framework.Business.EntityResourceManager.Contracts.EntityResourceManager(); IManagerBatchItem _managerBatchItem = new ManagerBatchItem(); var result = _iSettingManager.GetSettingsByContext(tenantId, SettingContextTypeEnum.EMAIL); if (result != null) { EmailSenderOptions options = Newtonsoft.Json.JsonConvert.DeserializeObject <EmailSenderOptions>(result.Content); //Get Batch Item var allBatchItems = _managerBatchItem.GetBatchItems(tenantId, new Guid(batchType.InternalId.Value), (batchType.ItemRetryCount.Value.Length > 0 ? (int?)Int32.Parse(batchType.ItemRetryCount.Value) : (int?)null)); foreach (var allBatchItem in allBatchItems) { try { var queryContext = new QueryContext { Fields = "Body,Sender,Recipient,Subject,Id" }; DataTable templatedt = _queryManager.GetResultById(tenantId, "email", allBatchItem.ReferenceId, queryContext); _managerBatchItem.BatchItemUpdateStartTime(tenantId, allBatchItem.BatchItemId); var email = EntityMapper <Email> .Mapper(templatedt); SendEmail(tenantId, allBatchItem, email.Recipient.Value, email.Subject.Value, email.Body.Value, options); //Update batch History _managerBatchItem.BatchHistoryCreate(tenantId, new BatchItemHistory { BatchHistoryId = Guid.NewGuid(), BatchItemId = allBatchItem.BatchItemId, EntityId = allBatchItem.EntityId, ReferenceId = allBatchItem.ReferenceId, Status = EmailEnum.Send, RunTime = allBatchItem.NextRunTime }); //Update Email Status _iEntityResourceManager.UpdateSpecificField(tenantId, "email", allBatchItem.ReferenceId, "Status", ((int)EmailEnum.Send).ToString()); } catch (System.Exception ex) { _log.Error("An error has occurred while sending email", ex.Message); //Update batch History _managerBatchItem.BatchHistoryCreate(tenantId, new BatchItemHistory { BatchHistoryId = Guid.NewGuid(), BatchItemId = allBatchItem.BatchItemId, EntityId = allBatchItem.EntityId, ReferenceId = allBatchItem.ReferenceId, Status = EmailEnum.Fail, RunTime = allBatchItem.NextRunTime, FailedReason = ex.Message }); //Update Batch item status _managerBatchItem.BatchItemUpdate(tenantId, (batchType.ItemRetryCount.Value.Length > 0 ? (int?)Int32.Parse(batchType.ItemRetryCount.Value) : (int?)null), new BatchItem { BatchItemId = allBatchItem.BatchItemId, Status = EmailEnum.Fail, FailedReason = ex.Message }); //Update Email Status _iEntityResourceManager.UpdateSpecificField(tenantId, "email", allBatchItem.ReferenceId, "Status", ((int)EmailEnum.Fail).ToString()); throw ex; } //Update Batch item status _managerBatchItem.BatchItemUpdateStatus(tenantId, new BatchItem { Status = EmailEnum.Send, BatchItemId = allBatchItem.BatchItemId }); } } else { _log.Error("Email gateway not configured"); } } catch (System.Exception ex) { _log.Error("Email send failed", ex.Message); } }
BatchTypeReturnMessage IBatchTypes.OnExecute(dynamic obj) { try { var tenantId = (Guid)obj[0]; BatchTypeInfo batchType = (BatchTypeInfo)obj[1]; var result = _iSettingManager.GetSettingsByContext(tenantId, SettingContextTypeEnum.EMAIL); if (result != null) { EmailSenderOptions options = Newtonsoft.Json.JsonConvert.DeserializeObject <EmailSenderOptions>(result.Content); //get mail in draft mode var queryFilter1 = new List <QueryFilter>(); queryFilter1.Add(new QueryFilter { FieldName = "CurrentWorkFlowStep", Operator = "Equal", Value = WorkFlowEngine.Draft.ToString() }); queryFilter1.Add(new QueryFilter { FieldName = "TenantId", Operator = "Equal", Value = tenantId.ToString() }); // queryFilter1.Add(new QueryFilter{FieldName="CurrentWorkFlowStep",Operator="Equal" ,Value= WorkFlowEngine.Fail.ToString()}); var queryContext1 = new QueryContext { Fields = "Date,Recipient,Sender,Subject,Body,CurrentWorkFlowStep", Filters = queryFilter1, PageSize = 100, PageIndex = 1 }; var emails = _iEntityResourceManager.GetResult(tenantId, "email", queryContext1); var emailMapped = EntityMapper <Email> .Mappers(emails); if (emailMapped.Count > 0) { foreach (var emailMap in emailMapped) { var currentWorkFlowId = Guid.Empty; if (emailMap.CurrentWorkFlowStep.Value == "Draft") { currentWorkFlowId = WorkFlowEngine.Draft; } else if (emailMap.CurrentWorkFlowStep.Value == "Send") { currentWorkFlowId = WorkFlowEngine.Sent; } else if (emailMap.CurrentWorkFlowStep.Value == "Failure") { currentWorkFlowId = WorkFlowEngine.Fail; } else if (emailMap.CurrentWorkFlowStep.Value == "Cancel") { currentWorkFlowId = WorkFlowEngine.Cancel; } var nextSteps = _workFlow.GetNextPossibleSteps(tenantId, "email", "Standard", currentWorkFlowId); var nextTransition = (from nextStep in nextSteps where nextStep.TransitionType.Id == WorkFlowEngine.Sent select nextStep).FirstOrDefault(); try { SendEmail(emailMap.Recipient.Value, emailMap.Subject.Value, emailMap.Body.Value, options); } catch (System.Exception ex) { _log.Error("An error has occurred while sending email", ex.Message); nextTransition = (from nextStep in nextSteps where nextStep.TransitionType.Id == WorkFlowEngine.Fail select nextStep).FirstOrDefault(); } var transitionWapper = new TransitionWapper { EntityName = "email", SubTypeName = "Standard", StepId = nextTransition.InnerStepId, RefId = Guid.Parse(emailMap.InternalId.Value), CurrentTransitionType = currentWorkFlowId, NextTransitionType = nextTransition.TransitionType.Id }; _workFlow.ManageTransitionFirstStep(tenantId, transitionWapper); } } } else { _log.Error("Email gateway not configured"); } } catch (System.Exception ex) { _log.Error("Email send failed", ex.Message); } return(new BatchTypeReturnMessage { NoDataFound = true }); }