【问题标题】:Send an email with data from a method发送包含来自方法的数据的电子邮件
【发布时间】:2014-01-28 14:03:48
【问题描述】:

我想发送一封电子邮件,其中包含从 Nopcommerce MVC/ASP 中的“DailyBestSellersReport”方法收集的数据:

public IList<BestsellersReportLine> DailyBestSellersReport(
            int recordsToReturn = 5, int orderBy = 1, int groupBy = 1)
        {
            var yesterday = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
            var earliest = new DateTime(yesterday.Year, yesterday.Month, yesterday.Day, 0, 0, 0);
            var latest = earliest.Add(new TimeSpan(1, 0, 0, 0, -1));
            var CurrentDay = DateTime.Now;
            var DayBefore = DateTime.Now.AddDays(-1);


                var query1 = from opv in _opvRepository.Table
                         where earliest <= CurrentDay && latest >= DayBefore
                         join o in _orderRepository.Table on opv.OrderId equals o.Id
                         join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
                         join p in _productRepository.Table on pv.ProductId equals p.Id
                         select opv;


                var query2 = groupBy == 1 ?
                    //group by product variants
                       from opv in query1
                       group opv by opv.ProductVariantId into g
                       select new
                       {
                           EntityId = g.Key,
                           TotalAmount = g.Sum(x => x.PriceExclTax),
                           TotalQuantity = g.Sum(x => x.Quantity),
                       }
                       :
                    //group by products
                       from opv in query1
                       group opv by opv.ProductVariant.ProductId into g
                       select new
                       {
                           EntityId = g.Key,
                           TotalAmount = g.Sum(x => x.PriceExclTax),
                           TotalQuantity = g.Sum(x => x.Quantity),
                       }
                       ;

                switch (orderBy)
                {
                    case 1:
                        {
                            query2 = query2.OrderByDescending(x => x.TotalQuantity);
                        }
                        break;
                    case 2:
                        {
                            query2 = query2.OrderByDescending(x => x.TotalAmount);
                        }
                        break;
                    default:
                        throw new ArgumentException("Wrong orderBy parameter", "orderBy");
                }

                if (recordsToReturn != 0 && recordsToReturn != int.MaxValue)
                    query2 = query2.Take(recordsToReturn);

                var result = query2.ToList().Select(x =>
                {
                    var reportLine = new BestsellersReportLine()
                    {
                        EntityId = x.EntityId,
                        TotalAmount = x.TotalAmount,
                        TotalQuantity = x.TotalQuantity
                    };
                    return reportLine;
                }).ToList();

                return result;

        }

我发现下面的方法发送邮件:

[NopHttpsRequirement(SslRequirement.No)]
public ActionResult ContactUs()
{
    var model = new ContactUsModel()
    {
        Email = _workContext.CurrentCustomer.Email,
        FullName = _workContext.CurrentCustomer.GetFullName(),
        DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage
    };
    return View(model);
}
[HttpPost, ActionName("ContactUs")]
[CaptchaValidator]
public ActionResult ContactUsSend(ContactUsModel model, bool captchaValid)
{
    //validate CAPTCHA
    if (_captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage && !captchaValid)
    {
        ModelState.AddModelError("", _localizationService.GetResource("Common.WrongCaptcha"));
    }

    if (ModelState.IsValid)
    {
        string email = model.Email.Trim();
        string fullName = model.FullName;
        string subject = string.Format(_localizationService.GetResource("ContactUs.EmailSubject"), _storeInformationSettings.StoreName);

        var emailAccount = _emailAccountService.GetEmailAccountById(_emailAccountSettings.DefaultEmailAccountId);
        if (emailAccount == null)
            emailAccount = _emailAccountService.GetAllEmailAccounts().FirstOrDefault();

        string from = null;
        string fromName = null;
        string body = Core.Html.HtmlHelper.FormatText(model.Enquiry, false, true, false, false, false, false);
        //required for some SMTP servers
        if (_commonSettings.UseSystemEmailForContactUsForm)
        {
            from = emailAccount.Email;
            fromName = emailAccount.DisplayName;
            body = string.Format("<strong>From</strong>: {0} - {1}<br /><br />{2}", 
                Server.HtmlEncode(fullName), 
                Server.HtmlEncode(email), body);
        }
        else
        {
            from = email;
            fromName = fullName;
        }
        _queuedEmailService.InsertQueuedEmail(new QueuedEmail()
        {
            From = from,
            FromName = fromName,
            To = emailAccount.Email,
            ToName = emailAccount.DisplayName,
            Priority = 5,
            Subject = subject,
            Body = body,
            CreatedOnUtc = DateTime.UtcNow,
            EmailAccountId = emailAccount.Id
        });

        model.SuccessfullySent = true;
        model.Result = _localizationService.GetResource("ContactUs.YourEnquiryHasBeenSent");

        //activity log
        _customerActivityService.InsertActivity("PublicStore.ContactUs", _localizationService.GetResource("ActivityLog.PublicStore.ContactUs"));

        return View(model);
    }

    model.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage;
    return View(model);
}

但是我不知道我需要哪些部件,以及如何从“DailyBestSellersReport”传递数据并将其作为电子邮件发送。

有这方面经验的人吗?

谢谢

//克里斯

【问题讨论】:

    标签: c# asp.net-mvc email nopcommerce


    【解决方案1】:

    如果您查看Nop.Service-&gt;Message 中的MessageTokenProvider,您会发现用于创建 Html 表作为电子邮件模板标记的方法。同样可以应用于您的情况,即您可以使用 result 作为方法的参数,此方法将生成 Html 表。您可以将此作为令牌添加到您的电子邮件中。

    如有任何疑问,请回来!

    【讨论】:

    • 您的意思是创建新令牌和新消息模板?我认为在没有这个过程的情况下发送电子邮件会更简单,就像上面不使用消息模板的“ContactUs”方法一样。谢谢
    • 是的,您也可以这样做,您只需要创建 html 表然后将其作为电子邮件正文发送但最好有 Message 模板,因为它为您提供了灵活性并使您的代码更易于维护
    • 你会采取什么步骤来完成这个?例如: 1、在DB MessageTemplates 表中添加一个新模板。 2:添加令牌。 3:在WorkFlowMessageService ..etc..中创建消息模板?
    • 没错。您需要使用所需的令牌创建消息模板。然后,您可以查看此模板并在 yoursite.com/Admin/MessageTemplate/List 进行编辑并向其中添加令牌。然后可以用从 WorkFlowMessaginService 调用的 MessageTokenProvider 服务中的实际数据替换这些令牌。请检查下订单或其他电子邮件模板的示例!
    猜你喜欢
    • 2013-08-20
    • 2011-09-29
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多