public ActionResult SendTestEmail(CampaignModel model) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageCampaigns)) { return(AccessDeniedView()); } var campaign = _campaignService.GetCampaignById(model.Id); if (campaign == null) { return(RedirectToAction("List")); } PrepareCampaignModel(model, campaign, false); try { var emailAccount = _emailAccountService.GetDefaultEmailAccount(); if (emailAccount == null) { throw new SmartException(T("Common.Error.NoEmailAccount")); } var subscription = _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmail(model.TestEmail); if (subscription != null) { // there's a subscription. let's use it var subscriptions = new List <NewsLetterSubscription>(); subscriptions.Add(subscription); _campaignService.SendCampaign(campaign, emailAccount, subscriptions); } else { //no subscription found _campaignService.SendCampaign(campaign, emailAccount, model.TestEmail); } NotifySuccess(T("Admin.Promotions.Campaigns.TestEmailSentToCustomers"), false); return(View(model)); } catch (Exception exc) { NotifyError(exc, false); } //If we got this far, something failed, redisplay form return(View(model)); }
/// <summary> /// Sends SMS /// </summary> /// <param name="text">SMS text</param> /// <returns>Result</returns> public bool SendSms(string text) { try { var emailAccount = _emailAccountService.GetDefaultEmailAccount(); if (emailAccount == null) { throw new Exception(_services.Localization.GetResource("Common.Error.NoEmailAccount")); } var queuedEmail = new QueuedEmail { Priority = 5, From = emailAccount.ToEmailAddress(), To = _verizonSettings.Email, Subject = _services.StoreContext.CurrentStore.Name, Body = text, CreatedOnUtc = DateTime.UtcNow, EmailAccountId = emailAccount.Id }; _queuedEmailService.InsertQueuedEmail(queuedEmail); return(true); } catch (Exception exception) { _logger.ErrorsAll(exception); return(false); } }
protected EmailAccount GetEmailAccountOfMessageTemplate(MessageTemplate messageTemplate, int languageId) { var accountId = messageTemplate.GetLocalized(x => x.EmailAccountId, languageId); var account = _emailAccountService.GetEmailAccountById(accountId); if (account == null) { account = _emailAccountService.GetDefaultEmailAccount(); } if (account == null) { throw new SmartException(T("Common.Error.NoEmailAccount")); } return(account); }
protected EmailAccount GetEmailAccountOfMessageTemplate(MessageTemplate messageTemplate, int languageId) { // Note that the email account to be used can be specified separately for each language, that's why we use GetLocalized here. var accountId = messageTemplate.GetLocalized(x => x.EmailAccountId, languageId); var account = _emailAccountService.GetEmailAccountById(accountId); if (account == null) { account = _emailAccountService.GetDefaultEmailAccount(); } if (account == null) { throw new SmartException(T("Common.Error.NoEmailAccount")); } return(account); }
private async Task SendCompletionEmail(ImportProfile profile, DataImporterContext ctx) { var emailAccount = _emailAccountService.GetDefaultEmailAccount(); var result = ctx.ExecuteContext.Result; var store = _services.StoreContext.CurrentStore; var storeInfo = $"{store.Name} ({store.Url})"; using var psb = StringBuilderPool.Instance.Get(out var body); body.Append(T("Admin.DataExchange.Import.CompletedEmail.Body", storeInfo)); if (result.LastError.HasValue()) { body.AppendFormat("<p style=\"color: #B94A48;\">{0}</p>", result.LastError); } body.Append("<p>"); body.AppendFormat("<div>{0}: {1} · {2}: {3}</div>", T("Admin.Common.TotalRows"), result.TotalRecords, T("Admin.Common.Skipped"), result.SkippedRecords); body.AppendFormat("<div>{0}: {1} · {2}: {3}</div>", T("Admin.Common.NewRecords"), result.NewRecords, T("Admin.Common.Updated"), result.ModifiedRecords); body.AppendFormat("<div>{0}: {1} · {2}: {3}</div>", T("Admin.Common.Errors"), result.Errors, T("Admin.Common.Warnings"), result.Warnings); body.Append("</p>"); var message = new MailMessage { From = new(emailAccount.Email, emailAccount.DisplayName), Subject = T("Admin.DataExchange.Import.CompletedEmail.Subject").Value.FormatInvariant(profile.Name), Body = body.ToString() }; if (_contactDataSettings.WebmasterEmailAddress.HasValue()) { message.To.Add(new(_contactDataSettings.WebmasterEmailAddress)); } if (!message.To.Any() && _contactDataSettings.CompanyEmailAddress.HasValue()) { message.To.Add(new(_contactDataSettings.CompanyEmailAddress)); } if (!message.To.Any()) { message.To.Add(new(emailAccount.Email, emailAccount.DisplayName)); } await using var client = await _mailService.ConnectAsync(emailAccount); await client.SendAsync(message, ctx.CancelToken); //_db.QueuedEmails.Add(new QueuedEmail //{ // From = emailAccount.Email, // To = message.To.First().Address, // Subject = message.Subject, // Body = message.Body, // CreatedOnUtc = DateTime.UtcNow, // EmailAccountId = emailAccount.Id, // SendManually = true //}); //await _db.SaveChangesAsync(); }
public async Task <ActionResult> SendTestMail(string token, string to) { var model = GetPreviewMailModel(token); if (model == null) { return(Json(new { success = false, message = "Preview result not available anymore. Try again." })); } try { var account = _emailAccountService.GetEmailAccountById(model.EmailAccountId) ?? _emailAccountService.GetDefaultEmailAccount(); var msg = new EmailMessage(to, model.Subject, model.Body, model.From); await _emailSender.SendEmailAsync(new SmtpContext(account), msg); return(Json(new { success = true })); } catch (Exception ex) { NotifyError(ex); return(Json(new { success = false, message = ex.Message })); } }