public async void SendMailFromMandril(DateTime SecheduleDate, bool IsInstant) { MassMailHelper conMassMail = new MassMailHelper(_httpContextAccessor, _env, _memoryCache); SendsEmail sendsEmail = await conMassMail.GetMailAndUserToSendMail(SecheduleDate.ToString()); MailSendInfo mail = sendsEmail.Mail; List <MailMergeVar> lstMergeVars = new List <MailMergeVar>(); EmailMessage message = new EmailMessage(); if (mail != null) { message.from_email = mail.MailFrom; message.from_name = mail.MailFrom; message.subject = mail.Subject; List <EmailAddress> toEmails = new List <EmailAddress>(); message.html = this.ConvertAllMergeTagsToMandrillsTags(mail.MessageBody); foreach (UserInfo subcriber in sendsEmail.Subscribers) { if (!string.IsNullOrEmpty(subcriber.EmailAddress)) { toEmails.Add(new EmailAddress(subcriber.EmailAddress, subcriber.FullName, "to")); lstMergeVars.Add(new MailMergeVar { rcpt = subcriber.EmailAddress, vars = this.GetSubscriberMergeVars(subcriber.TokenValues, mail.TokenKeys) }); } } message.to = toEmails; message.merge_vars = lstMergeVars; FinalEmail finalEmail = new FinalEmail(); finalEmail.async = true; finalEmail.message = message; if (!IsInstant) { finalEmail.send_at = SecheduleDate.ToString(MandrilConfiguration.DATE_TIME_FORMAT_STRING); } string ApiKey = await MailSettingController.GetSettingByKey(MailSettingKey.MandrilAPIKey); MandrilHelper mandrilMailHelper = new MandrilHelper(ApiKey); List <EmailResult> emailResults = await mandrilMailHelper.SendMessage(finalEmail); MassMailProvider massMailProvider = new MassMailProvider(); if (emailResults != null) { await massMailProvider.UpdateStausOfFailMail(mail.MassMailID, string.Empty); foreach (EmailResult objResult in emailResults) { await massMailProvider.AddMailSubscriberStatus(mail.MassMailID, objResult.Email, (int)objResult.Status, 1, objResult.RejectReason); } } } }
internal async Task <SendsEmail> GetMailAndUserToSendMail(DateTime SecheduleDate) { try { const string sp = "[dbo].[usp_Wb_MassMail_GetMailForSend]"; List <SQLParam> Param = new List <SQLParam> { new SQLParam("@ScheduleDate", SecheduleDate) }; SendsEmail objSendMail = new SendsEmail(); SQLGetListAsync handler = new SQLGetListAsync(); DataSet ds = await handler.ExecuteAsDataSetAsync(sp, Param); if (ds != null && ds.Tables.Count > 0) { MailSendInfo objMail = DataSourceHelper.FillObject <MailSendInfo>(ds.Tables[0].CreateDataReader()); IList <UserInfo> userInfo = DataSourceHelper.FillCollection <UserInfo>(ds.Tables[1]); if (objMail != null) { objSendMail.Mail = objMail; } if (userInfo != null) { objSendMail.Subscribers = userInfo; } return(objSendMail); } else { return(null); } } catch { throw; } }