发送包含方法数据的电子邮件

时间:2014-01-28 14:03:49

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

我想发送一封电子邮件,其中包含从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”传递数据并将其作为电子邮件发送。

有这方面经验的Annyone?

THX

// CHRISS

1 个答案:

答案 0 :(得分:2)

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

如果有任何疑问,请回去!